12

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?

Bolot NeznaJu
  • 155
  • 1
  • 1
  • 7

1 Answers1

31

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...
}
Timothy Moose
  • 9,895
  • 3
  • 33
  • 44
  • thanks!can you post an example how it works with the appropriate identifier based in index path? – Bolot NeznaJu Jul 25 '13 at 17:07
  • i've done it like you said. And i get an error. "Tableview datasource must return a cell from tableview:cellforrowatindexpath" Maybe i forgot something? I have linked the cell in the storyboard to custom cell, and the labels also. ...NSInteger row = [indexPath row]; NSInteger row = [indexPath row]; NSString *identifier; CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (row == 0) {identifier =@"ThirdCell"; cell.clubNameLabel.text =@"%@",[clubname objectAtIndex:row]; }return cell;.... – Bolot NeznaJu Jul 25 '13 at 20:27
  • You're either not getting a cell back from `dequeueReusableCellWithIdentifier` or you're not returning it from `cellForRowAtIndexPath`. From your comment, I would guess it is the former because you're calling dequeue before setting a value for `identifier`. In any case, you should put a breakpoint in `cellForRowAtIndexPath` and step through to see where things go wrong. – Timothy Moose Jul 25 '13 at 20:38