Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 754
Bolot NeznaJu Avatar asked Jul 25 '13 16:07

Bolot NeznaJu


1 Answers

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...
}
like image 184
Timothy Moose Avatar answered Oct 12 '22 23:10

Timothy Moose