2013-07-25 12 views
11

Voglio aggiungere 2 o più celle personalizzate differenti in una Tableview, usando lo storyboard. so come aggiungere celle diffrenti senza storyboard. Lo faccio sempre in questo modo:Come aggiungere diverse celle personalizzate in una TableView con Storyboard?

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]; 
} 

E ora la mia domanda: come posso farlo con lo storyboard? Per aggiungere una cella personalizzata è possibile. Ma non posso aggiungere due celle differenti. Mi potete aiutare per favore?

risposta

30

Nell'ispettore attributi per la vista tabella, selezionare "Prototipi dinamici" e in basso che selezionare il numero di celle prototipo. Assegna a ogni cella un identificatore diverso e, quando si deselezionano le celle in cellForRowAtIndexPath, utilizzare l'identificatore appropriato in base a 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

grazie! Puoi pubblicare un esempio come funziona con l'identificativo appropriato in base a percorso dell'indice? –

+0

grazie mille :) –

+0

l'ho fatto come hai detto tu. E ottengo un errore "L'origine dati Tableview deve restituire una cella da tableview: cellforrowatindexpath" Forse ho dimenticato qualcosa? Ho collegato la cella nello storyboard alla cella personalizzata e anche le etichette. ... NSInteger row = [indexPath row]; Riga NSInteger = [riga IndexPath]; Identificatore NSString *; CustomCell * cell = [tableView dequeueReusableCellWithIdentifier: identificatore]; if (row == 0) {identifier = @ "ThirdCell"; cell.clubNameLabel.text = @ "% @", [nome del club objectAtIndex: row]; } cella di restituzione; .... –

Problemi correlati