I\'ve implemented a custom table view cell class that inherit from UITableViewCell
. The tableview contains a background image, so I want cell\'s background to b
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.
I found that none of the above worked from XCode 5.1.1, iOS 7.1.
If you are using Interface Builder with prototype cells, select the prototype cell, then from the attributes selector in the View section, change the Background form default to Clear Color:
This seems to work fine. None of the above code changes are needed, either--this is a pure IB solution.
Write this in your cellForRowAtIndexPath method before return cell;
cell.backgroundColor = [UIColor clearColor];
For me setting
cell.backgroundColor = cell.contentView.backgroundColor;
either in tableView:willDisplayCell:forRowAtIndexPath:
or tableView:cell:forRowAtIndexPath:
did the job.
This is because I set the contentView's background colour in Interface Builder as desired.
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. :)
The accepted answer did not fix the problem for me. I had to do this:
From the table's data source class cellForRowAtIndexPath method:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor clearColor]; //Make cell transparent