简体   繁体   中英

is this code OK for setting UITableViewCell background color?

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:

  1. Would this be the best re performance in the sense that it seems like this code will be called quite a lot? ie 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
  2. 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 UITableViewCell documentation even explicitly states that background colors should be set in tableView:willDisplayCell:forRowAtIndexPath: as opposed to tableView:cellForRowAtIndexPath: . Don't fear, you are on the right path sir.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM