How to add different Custom Cells in one TableView with Storyboard?

Bolot NeznaJu picture Bolot NeznaJu · Jul 25, 2013 · Viewed 18.2k times · Source

i want to add 2 or more diffrent Custom cells in one Tableview, by using storyboard. i know how to add diffrent cells without storyboard. I always do this by this way:

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

And now my question: How i can do this with storyboard? To add one custom cell is possible. But i can not add two diffrent cell. Can you help me please?

Answer

Timothy Moose picture Timothy Moose · Jul 25, 2013

In the attributes inspector for the table view, select "Dynamic Prototypes" and below that select the number of prototype cells. Give each cell a different identifier and when dequeuing cells in cellForRowAtIndexPath, use the appropriate identifier based on 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...
}