Can't set background color of UITableViewCell in IB

后端 未结 2 1197
無奈伤痛
無奈伤痛 2020-12-09 08:23

I have created a bunch of cells which I reuse in a table view. All of those cells just have different UILabels inside them and some UIImageViews (n

2条回答
  •  心在旅途
    2020-12-09 08:44

    To change the background color of the cell, you have to do it in the willDisplayCell:forRowAtIndexPath: method. This is mentioned in the UITableViewCell documentation near the bottom of the Overview. So you need:

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        cell.backgroundColor = [UIColor redColor];  
    }
    

    The contentView is just the recommended subview to put custom controls in so that the cell gets layed out properly when table editing is going on.

提交回复
热议问题