I’ve read quite a few different ways to tackle the problem of setting the UITableViewCell color. Was just after a quick confirmation that the code/approach below is fine re a best practice / performance point of view?
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];
}
Some notes/questions associated with this:
- Would this be the best re performance in the sense that it seems like this code will be called quite a lot? i.e. as opposed to if the background color was just set once for the cell design, say in IB (which I know doesn’t quite work) see here
- I see some sample code that loops through the various sub-views of a cell to set them, but this wouldn’t normally be required? I’ve tried this code above and it seemingly works ok.
You are correct, this is the way to do it. The
UITableViewCelldocumentation even explicitly states that background colors should be set intableView:willDisplayCell:forRowAtIndexPath:as opposed totableView:cellForRowAtIndexPath:. Don’t fear, you are on the right path sir.