Set background color of UITableViewCell Set background color of UITableViewCell objective-c objective-c

Set background color of UITableViewCell


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

Using this UITableViewDelegate method, you can set the color of cells to different colors. Note that Apple explicitly advise you to make changes to the backgroundColor property within the tableView:willDisplayCell:ForRowAtIndexPath: method in the docs, which state:

If you want to change the background color of a cell, do so in the tableView:willDisplayCell:forRowAtIndexPath: method of your table view delegate

Indeed, in iOS 6, changes to the property from anywhere else (like the tableView:cellForRowAtIndexPath: method) would have no effect at all. That no longer seems to be the case in iOS 7, but Apple's advice to modify the property from within tableView:willDisplayCell:ForRowAtIndexPath: remains (without any explanation).

For alternating colors, do something like this example:

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


I struggled with this one for a little while too and resorted to creating a custom image with the accessory. But I just found this solution that works well and doesn't require a custom image. The trick is to change the cell's backgroundView color not the backgroundColor.

UIView *myView = [[UIView alloc] init];if (indexPath.row % 2) {    myView.backgroundColor = [UIColor whiteColor];} else {    myView.backgroundColor = [UIColor blackColor];}cell.backgroundView = myView;

No need to change the accessoryView or contentView background colors. They'll follow automatically.


Note for 2014. Very typically you wold use -(void)setSelected:(BOOL)selected animated:(BOOL)animated

So, you'd have a custom cell class, and you'd set the colours for the normal/selected like this...

HappyCell.h@interface HappyCell : UITableViewCell@property (strong, nonatomic) IBOutlet UILabel *mainLabel;etc...@endHappyCell.m@implementation HappyCell-(id)initWithStyle:(UITableViewCellStyle)style      reuseIdentifier:(NSString *)reuseIdentifier    {    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];    if (self)        {        }    return self;    }-(void)awakeFromNib    {    }- (void)setSelected:(BOOL)selected animated:(BOOL)animated    {    [super setSelected:selected animated:animated];    if(selected)        {        self.backgroundColor = [UIColor redColor];        .. other setup for selected cell        }    else        {        self.backgroundColor = [UIColor yellowColor];        .. other setup for normal unselected cell        }    }@end// to help beginners.......// in your table view class, you'd be doing this...-(NSInteger)tableView:(UITableView *)tableView      numberOfRowsInSection:(NSInteger)section    {    return yourDataArray.count;    }-(UITableViewCell *)tableView:(UITableView *)tv      cellForRowAtIndexPath:(NSIndexPath *)indexPath    {    NSInteger thisRow = indexPath.row;    ContentsCell *cell = [tv      dequeueReusableCellWithIdentifier:@"cellName"      forIndexPath:indexPath];     // "cellName" must be typed in on the cell, the storyboard    // it's the "identifier", NOT NOT NOT the restorationID    [cell setupForNumber: thisRow];    cell.mainLabel.text = yourDataArray[ thisRow ][@"whatever"];    cell.otherLabel.text = yourDataArray[ thisRow ][@"whatever"];    return cell;    }

hope it helps someone.


This worked for me:

cell.contentView.backgroundColor = [UIColor darkGreyColor];