2013-07-25 17 views
11

Chcę dodać 2 lub więcej różnych niestandardowych komórek w jednym widoku tabeli, używając scenorysów. Wiem, jak dodawać różne komórki bez storyboardu. Zawsze robię to w następujący sposób:Jak dodać różne niestandardowe komórki w jednym widoku tabeli za pomocą scenorysu?

static NSString *CellIdentifier = @"Cell"; 
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
//pictureCell = [[DetailPictureCell alloc]init];//(DetailPictureCell *)[tableView dequeueReusableCellWithIdentifier: CellIdentifier]; 
pictureCell.header = true; 
[pictureCell setHeader]; 
if (cell == nil) { 
    if ([indexPath row] == 0) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HeaderAngebotViewCell" owner:self options:nil]; 
     NSLog(@"New Header Cell"); 
} 
    if([indexPath row] ==1){ 
    NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"productCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
} 

A teraz moje pytanie: jak mogę to zrobić przy użyciu storyboardu? Aby dodać jedną komórkę niestandardową, jest to możliwe. Ale nie mogę dodać dwóch różnych komórek. Czy możesz mi pomóc?

Odpowiedz

30

W inspektorze atrybutów widoku tabeli wybierz "Dynamiczne prototypy", a poniżej wybierz liczbę komórek prototypowych. Daj każdej komórce inny identyfikator, a podczas usuwania komórek z cellForRowAtIndexPath użyj odpowiedniego identyfikatora opartego na indexPath.

enter image description here

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *identifier; 
    if (indexPath.row == 0) { 
     identifier = @"OneCellId"; 
    } else if (indexPath.row == 1) { 
     identifier = @"OtherCellId"; 
    } 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 

    //configure cell... 
} 
+0

dzięki! Można zakładać przykład jak to działa z odpowiednim identyfikatorem indeksu opartego na ścieżce? –

+0

dziękuję bardzo :) –

+0

Zrobiłem to tak, jak powiedziałeś. I pojawia się błąd. "Źródło danych Tableview musi zwracać komórkę z tableview: cellforrowatindexpath" Może coś zapomniałem? Połączyłem komórkę w storybooku z komórką niestandardową, a także z etykietami. ... NSInteger wiersz = [wiersz indexPath]; NSInteger wiersz = [wiersz indexPath]; NSString * identyfikator; CustomCell * cell = [tableView dequeueReusableCellWithIdentifier: identyfikator]; jeśli (wiersz == 0) {identyfikator = @ "ThirdCell"; cell.clubNameLabel.text = @ "% @", [nazwa klubu objectAtIndex: row]; } Zwróć komórkę; .... –

Powiązane problemy