0
votes

I'm trying to implement two custom cells, customCellView1 and customCellView2 in the willDisplayCell method - which seems only to support one cell. Is there a way to rewrite this to include two? Thank you.

  • (void)tableView: (UITableView *)tableView willDisplayCell: (customCellView1 *)cell forRowAtIndexPath: (NSIndexPath *)indexPath {
2

2 Answers

2
votes

It all lies ultimately in your dequeue call in cellForRowAtIndexPath:. You get to specify the reuse identifier. So if this row needs a type 1 cell, you give the reuse identifier for type 1, but if this row needs a type 2 cell, you give the reuse identifier for type 2. And you branch according to which one it is.

Well, the reuse identifier is attached to the cell. So now when you get to willDisplayCell:, the same thing is true: you cast as a UITableViewCell* to start with, but then check the cell's reuse identifier and branch according to which type it is.

1
votes

Check for the class of the cell in an if/else.

- (void)tableView:(UITableView *)tableView willDisplayCell: (UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell is kindOfClass:[CustomCellClass1 class]] ) {
       [(CustomCellClass1 *)cell doWhatever];
    }
    else if ([cell is kindOfClass:[CustomCellClass2 class]] ) {
       [(CustomCellClass2 *)cell doSomethingDifferent];
    }
}