Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell show white background and cannot be modified on iOS7

As Apple DOC said (UITableViewCell Class Reference):

... In iOS 7, cells have a white background by default; in earlier versions of iOS, cells inherit the background color of the enclosing table view. If you want to change the background color of a cell, do so in the tableView:willDisplayCell:forRowAtIndexPath: method of your table view delegate.

So for my case that to show cells with transparent background, just need to implement the delegate method in the table view controller like below:

- (void)tableView:(UITableView *)tableView
  willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
  [cell setBackgroundColor:[UIColor clearColor]];
}

Just Note: As @null said, "...there seems to be a bug in interface builder...", I'm not totally sure whether it does have the bug, but seems so cause his comment got several up votes. So there might something wrong if you use IB. :)


I investigated it a little bit and found out that the cell backgroundColor is set by the Appearance system. So if all cells in your application have clear background, the easiest solution would be:

[[UITableViewCell appearance] setBackgroundColor:[UIColor clearColor]];

And even if they have different background, clear color seems to be the most convenient as the default one.


Write this in your cellForRowAtIndexPath method before return cell;

cell.backgroundColor = [UIColor clearColor];

The default background color of an UITableViewCell in iOS 7 is white.

You have to set the backgroundColor property somewhere in your code. For example, set it after you newly created the cell.

cell.backgroundColor = [UIColor clearColor];

I actually found that the issue arose from the UITableView's Background color not being set to clear. If you change the UITableViewCell's Background color to clear and you are finding you still see white, make sure that the UITableView's background color is set to clear (or whatever you want).

[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundColor:[UIColor clearColor]];

In Swift

tableView.backgroundView = nil
tableView.backgroundColor = UIColor.clearColor()

This is definitely an iOS bug. In iOS 8, setting background color in Interface Builder works fine for iPhone, but in iPad it's always whiteColor. To fix it, in the cellForIndexPath data source message, putting this in:

cell.backgroundColor = cell.backgroundColor

And it will work. This is exactly why I said it's a bug since the code itself is pretty much bullsh*t, but it works.