views:

24

answers:

1

Is there a way to use multiple uitableviewcell classes with nib files in the tableViewController?

There's this great tutorial video for how to create custom cell classes to be used in a tableViewController at the cellForRowAtIndexPath function that I'm pasting here in case anyone wants to see.

In this example though, they are only using one type of reusable cell class. This is from my own project but it's essentially what the tutorial presents. "videoCellEntry" is a custom cell that I have created with nib file videoEntryCell.xib, "videoEntry" is the class for each cell that I am configuring, and "videos" is an array of videoEntry.

I'm assuming to use multiple nib files, I can put some conditions to choose which nib file I want and then call a different loadNibNamed: portion like so:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"videoCellId";

    videoEntryCell *cell = 
  (videoEntryCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        if(<condition 1>) {
          NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"videoEntryCell" owner:self options:nil];
        }
        if(<condition 2>) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"videoEntryCell2" owner:self options:nil];
        }
 cell = (videoEntryCell *)[nib objectAtIndex:0];
    }

 // Configure the cell.
 videoEntry *vid = [videos objectAtIndex:indexPath.row];
 [cell configureForVideoEntry:vid];

    return cell;
}

But can the tableViewController handle multiple cell nib files? Or is there a better way? And wouldn't each cell require a different CellIdentifier depending on its nib file?

A: 

Yes, you can use multiple Nib-Files and multiple CellIdentifiers in the TableViewController. Just put your condition before the CellIdentifier. That's the way I've done it.

Some example inside the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method:

First define the variable cell:

UITableViewCell *cell;

Now the if-Block:

if(<condition 1>) {
    static NSString *CellIdentifier = @"VideoCell1";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"videoEntryCell" owner:nil options:nil];
        for (id currentObject in topLevelObjects){
            if([currentObject isKindOfClass:[UITableViewCell class]]){
                cell = (UITableViewCell *) currentObject;
                break;
            }
        }
    }
    // customize your cell here
}

if(<condition 2>) {
    static NSString *CellIdentifier = @"VideoCell2";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"videoEntryCell2" owner:nil options:nil];
        for (id currentObject in topLevelObjects){
            if([currentObject isKindOfClass:[UITableViewCell class]]){
                cell = (UITableViewCell *) currentObject;
                break;
            }
        }
    }
    // customize your cell here
}

and finally:

return cell;

You can repeat the if-Block as often as you want. It's only important that you return a cell, which is not nil.

audience
Great! Thanks for the quick reply. I'm definitely trying this now.
Kennzo