2009-10-01 11 views
8

Używam niestandardowego UITableViewCell od stalówki. Widok akcesoriów to wskaźnik szczegółowych informacji. Problem polega na tym, że kolor tła obiektu UITableViewCell za widokiem akcesoriów nie jest renderowany (patrz obraz/źródło poniżej). Jakieś wskazówki? A oto kilka rzeczy, które próbowałem, ale nie działa:Jak uzyskać przezroczysty widok akcesoriów w UITableViewCell? (w/Screenshot)

Rzeczy, które nie działa:
- Ustawianie backgroundColor widoku akcesoriów do clearColor
- Ustawianie contentView.opaque komórki do FAŁSZ
- Ustawianie contentView.opaque tabeli widokiem na FAŁSZ
- ustalenie niż domyślny widok akcesoriów dla komórki


alt text http://www.chicknchoke.com/so/IMG_8028.png

-(void)showTablePrep 
    { 
     myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 480, 320, 416) style:UITableViewStylePlain]; 
     myTableView.dataSource = self; 
     myTableView.delegate = self; 
     myTableView.delaysContentTouches = FALSE; 
     myTableView.opaque = FALSE; 
     myTableView.rowHeight = 60; 

     [UIView beginAnimations:@"SlideUp" context:nil]; 
     [UIView setAnimationDuration:0.3f]; 

     [myTableView setCenter:CGPointMake(myTableView.center.x, myTableView.center.y-436)]; 
     [self.view addSubview:myTableView]; 
     [self.view bringSubviewToFront:myTableView]; 

     [UIView commitAnimations]; 


    } 




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    FriendsCell* cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCellID"]; 

    if (cell == nil){ 

     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FriendsCellView" owner:self options:nil]; 

     cell = (FriendsCell*)[nib objectAtIndex:0]; 

     if(indexPath.row % 2 == 0){ 

     cell.contentView.backgroundColor = [UIColor grayColor]; 


     }else{ 

     cell.contentView.backgroundColor = [UIColor lightGrayColor]; 


     } 
     cell.accessoryView.backgroundColor = [UIColor clearColor]; 
     cell.contentView.opaque = FALSE; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 


    if(f 

riendsCounter > indexPath.row){ 


      cell.titleLabel.text = @"Label"; 
      cell.descLabel.text = @"Description goes here"; 

     }else{ 

      cell.titleLabel.text = @""; 
      cell.descLabel.text = @""; 
      cell.accessoryType = UITableViewCellAccessoryNone; 

     } 

     } 

     return cell;  
    } 

Odpowiedz

11

pan rysunek kolor tła dla komórek nieprawidłowo. A UITableViewCell zostanie umieszczony tak, aby contentView i siedzieli obok siebie. (Dzieje się tak, aby contentView mógł przycinać swoją zawartość, aby nie nakładał się z widokiem akcesoriów) Problem nie polega na tym, że widok akcesoriów jest nieprzejrzysty, ponieważ szare tło nie jest po prostu rysowane za widokiem akcesoriów.

Poprawnym sposobem dostosowania tła narysowanego za UITableViewCell jest dostosowanie jego backgroundView. Nie próbowałem tego, ale ponieważ zmieniasz tylko kolor, możesz po prostu ustawić kolor backgroundColor na backgroundView na żądany kolor.

+0

Chciałem też wspomnieć, że istnieje naprawdę świetny przegląd dostosowywania 'UITableView' rysunek tutaj: http://cocoawithlove.com/2009/04/easy-custom-uitab leview-drawing.html – Alex

+7

To działało dla mnie: komórka.backgroundView = init [[UIView alloc] init]; cell.backgroundView.backgroundColor = [UIColor yellowColor]; – joshaidan

+1

@joshaidan Nie zapomnij zwolnić utworzonego widoku (chyba że używasz ARC): cell.backgroundView = [[[UIView alloc] init] autorelease]; – manicaesar

1

Znalazłem odpowiedź, rzucając okiem na subviews mojej niestandardowej komórki widoku tabeli.

Wygląda na to, że widok akcesoriów ma nad nim przycisk. Po znalezieniu tego przycisku w subviewsach i zmianie ich koloru, udało mi się zaktualizować kolor tła za przyciskiem akcesoriów.

<UIButton: 0x3b4d690; frame = (277 0; 43 75); opaque = NO; layer = <CALayer: 0x3b3e0b0>> 

for (UIView *aSubView in self.subviews) { 
    if ([aSubView isMemberOfClass:[UIButton class]]) { 
     aSubView.backgroundColor = [UIColor greenColor]; 
    } 
} 

Niestety jestem tylko w stanie osiągnąć tego przycisku w sposobie mojego widoku niestandardowego tabeli klasy komórek

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 

. Z powodzeniem używam tego w mojej aplikacji do wyświetlania innego koloru podświetlenia, gdy użytkownik wybierze komórkę. To powinno wskazać ci właściwy kierunek.

0

rozwiązać ten problem na iOS7 ale dodanie

[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

w

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

i zmodyfikować tło i inne bakcgrounds tutaj:

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath { 
    return YES; 
} 

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Add your Colour. 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [cell setBackgroundColor:[UIColor colorWithHexColor:HIGHLIGHT_COLOR]]; 
} 

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Reset Colour. 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [cell setBackgroundColor:[UIColor colorWithHexColor:UNHIGHLIGHT_COLOR]]; 
} 
Powiązane problemy