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

49,955

Solution 1

(try this)

Apple's IB's design is generic to be applied to multiple kinds of components, and is also targeted at a highly technical audience (developers). Hence it has not been designed (as an application) to fully customise the interface properly for each component/situation type. So in this case it is the case the ability to seemingly set the background-color is somewhat misleading.

Solution 2

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];

Solution 3

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];

Solution 4

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

Solution 5

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

This Works !!

Share:
49,955
Greg
Author by

Greg

Updated on January 11, 2020

Comments

  • Greg
    Greg over 4 years

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

    I note from some searching that the follow code set in your custom subclass of UITableViewController does work (see below):

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

    But I would still like to just understand why interface builder has a background color setting for the TableViewCell, which effectively doesn't seem to work?