Cell Backgrounds in iOS

Over the past few years, I have used a few different techniques to colour the backgrounds of UITableViewCell controls within iOS Apps. Today, I read about the correct way. I should have been using the method:

– (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

Put this into your delegate for the table view controller. An example method body is:

if([cell isMemberOfClass: [SessionCellVC class]]) {
cell.backgroundColor = [UIColor whiteColor];
}
else {
cell.backgroundColor = [UIColor redColor];
}

This method avoids silly tricks where you need to set the background of the text controls to clear so that they correctly show the background colour. Wish I had come across this sooner.

Comments are closed.