Chun
Chun

Reputation: 1988

iOS UITableView: changing background color of table overrides background color of cells

I have an issue where setting the background color of my tableView overrides the background colors I've set for my individual cells. To be more specific, I have a table with 3 rows: the top cell is dark gray, and the bottom 2 cells are red:

https://i.sstatic.net/Hx9bm.png

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIColor *sectionBackgroundColor = [UIColor colorWithRed:0.09f green:0.09f blue:0.09f alpha:1.0];
    UIColor *menuItemBackgroundColor = [UIColor redColor];

    if (indexPath.row == 0) {
        cell.backgroundColor = sectionBackgroundColor;
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.text = @"MENU";
        cell.textLabel.font= [UIFont systemFontOfSize:16.0];
        cell.selectionStyle = false;
    } else if (indexPath.row == 1) {
        cell.backgroundColor = menuItemBackgroundColor;
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.text = @"Schedule";
        cell.textLabel.font= [UIFont systemFontOfSize:18.0];
        cell.imageView.image = [UIImage imageNamed:@"Schedule.png"];
    } else if (indexPath.row == 2) {
        cell.backgroundColor = menuItemBackgroundColor;
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.text = @"Contacts";
        cell.textLabel.font= [UIFont systemFontOfSize:18.0];
        cell.imageView.image = [UIImage imageNamed:@"Contacts.png"];
    } else {
        cell.backgroundColor = [UIColor blackColor];
        cell.selectionStyle = false;
    }

}

What I'm trying to do is make all those empty, unused white cells on the bottom black. Based on my research, setting the tableView background color will do just that:

https://i.sstatic.net/2C1eg.png

tableView.backgroundColor = [UIColor blackColor];

However, as you noticed, this line of code changes the background colors of the top two cells to black too. I played around with this more and noticed a general pattern where every cell background color will be changed to black except the last one:

(see 3rd image in the above link)

Any ideas regarding what's happening here? Help would be appreciated!



Some things I've tried already that do not work (I would provide more links but my current rep doesn't allow me to):

A) Making the views in the cells opaque. Changing the colors of the different views in the cells.

cell.opaque = YES;
cell.backgroundView.opaque = YES;
cell.contentView.opaque = YES;
cell.backgroundColor = menuItemBackgroundColor;
cell.contentView.backgroundColor = menuItemBackgroundColor;
cell.backgroundView.backgroundColor = menuItemBackgroundColor;

B) Setting the backgroundView of tableView nil.

tableView.backgroundView = nil;

C) Setting the background color of the backgroundView of tableView.

tableView.backgroundView.backgroundColor = [UIColor blackColor];

Upvotes: 2

Views: 5103

Answers (2)

crishoj
crishoj

Reputation: 5927

The following workaround allows you to use the background colours specified in the storyboard, and avoid setting explicit colours in your code:

  • In dequeueReusableCellWithIdentifier:, while the background colour from the storyboard is still intact, save it, e.g. in a property of your custom UITableView subclass.
  • Later, in willDisplayCell:, restore the backgroundColor with the value you saved in in dequeueReusableCellWithIdentifier:.

Upvotes: 0

matt
matt

Reputation: 535944

setting the background color of my tableView overrides the background colors I've set for my individual cells

That's correct. Annoying, isn't it? :) The problem is really just that Either give the cell a colored backgroundView (much easier than it sounds), or else wait until tableView:willDisplayCell:forRowAtIndexPath:, and set the backgroundColor of your cells there.

Upvotes: 4

Related Questions