Prateek
Prateek

Reputation: 1754

UITableView Cells background color NOT changing?

I have a UITableView set up in two storyboards - one for iPhone and one for iPad.

I have set it up in the iPhone storyboard such that the cells have a clear background and so a background image shows through - this works perfectly fine.

HOWEVER, when I copy this view in to the iPad storyboard, for some reason the table cells have a white background even though all the settings are the same (clear colour still set in the builder).

What could be the reason for this?

EDIT - I've looked at this further and if I change the colour of a row to anything, it won't change - for some reason it remains white :/

Upvotes: 3

Views: 4282

Answers (4)

wailynnzaw
wailynnzaw

Reputation: 1

extension UITableViewCell {
    func setTransparent() {
        let bgView: UIView = UIView()
        bgView.backgroundColor = UIColor.clear
        self.backgroundView = bgView
        self.backgroundColor = UIColor.clear
        self.layer.backgroundColor = UIColor.clear.cgColor
        self.contentView.backgroundColor = UIColor.clear     
    }
}

and call that method in cellForRowAt

Upvotes: 0

Arpit Lokwani
Arpit Lokwani

Reputation: 127

You have to set the backgroundColor of the cell's contentView to your color

cell.contentView.backgroundColor = [UIColor yourcolor];

Upvotes: 2

Nicolas Buquet
Nicolas Buquet

Reputation: 3955

From iOS 7, UITableViewCell have a default white background.

Set: cell.contentView.backgroundColor = [UIColor clearColor]; and eventually : cell.contentView.backgroundView = nil;

And verifiy also your: tableview.backgroundColor = [UIColor clearColor]; tableview.backgroundView = nil;

Upvotes: 0

Prateek
Prateek

Reputation: 1754

Appears to be a known issue with this, and the fix is by adding the following to the .m file for your class:

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

Upvotes: 3

Related Questions