0

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 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

However, I would rethink your strategy here. Implementing willDisplayCell is almost never necessary. You should be able to do all the work in cellForRow.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.