Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't UITableViewCell background color work (set in interface builder)?

It's a bit late, but I just ran into this issue... Setting the background color on the contentView works fine:

cell.contentView.backgroundColor = [UIColor redColor];

What worked for me is creating my own UIView object to be set as the background view of the UITableViewCell. In the cellForRowAtIndexPath:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor orangeColor];
[cell setBackgroundView:bgview];

Trying to set the background colour of the UITableViewCell itself is not how you should be doing it. A table cell has a collection of five useful views you can access, one of which is backgroundView.

Recommended reading:

http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html

and then:

http://cocoawithlove.com/2010/12/uitableview-construction-drawing-and.html


You should always provide changes for cells in tableView:willDisplayCell:forRowAtIndexPath: instead of the tableView:cellForRowAtIndexPath: method, as per Apple Documentation.

This Works !!


[cell.textLabel setBackgroundColor:[UIColor clearColor]];
[cell.contentView setBackgroundColor:[UIColor redColor]];