UITableViewCell show white background and cannot be modified on iOS7

IosObjective CCocoa TouchIos7

Ios Problem Overview


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 be transparent. It looks great before iOS7.

However, in iOS7, the cell is always shown with a white background.


Even for Xcode7, 2015, there is a bug in storyboard: you have to set the background color of a cell in code.

Ios Solutions


Solution 1 - Ios

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. :)

Solution 2 - Ios

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.

Solution 3 - Ios

Write this in your cellForRowAtIndexPath method before return cell;

cell.backgroundColor = [UIColor clearColor];

Solution 4 - Ios

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

Solution 5 - Ios

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()

Solution 6 - Ios

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.

Solution 7 - Ios

It could be that your UITableViewCell is selected by default.. You can confirm this using Xcode 6s new visual debugger (or find out exactly which view is causing this white cell to appear).

enter image description here

Interestingly, after knowing this.. setting the background color of the selected cell to clear still didn't work.. doing some more research, turns out i just had to modify the selection style when i'm creating this custom cell:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // do customization here
    }
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    [self setBackgroundColor:[UIColor clearColor]];
    return self;
}

Solution 8 - Ios

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:

enter image description here

This seems to work fine. None of the above code changes are needed, either--this is a pure IB solution.

Solution 9 - Ios

The accepted answer did not fix the problem for me. I had to do this:

  1. In the interface builder, select the table view. Then from the attributes inspector, from the View section, set the Background to transparent (0% opacity).

enter image description here

  1. From the table's data source class cellForRowAtIndexPath method:

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
         reuseIdentifier:CellIdentifier];
     cell.backgroundColor = [UIColor clearColor]; //Make cell transparent
    

Solution 10 - Ios

Swift 1.2 Solution:

Just add an explicit definition for your cell's background color like so:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    // Configure the cell...
    cell.backgroundColor = UIColor.clearColor()

    return cell
}

Solution 11 - Ios

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.

Solution 12 - Ios

When adding UI objects to a cell, Xcode 5/IOS 7 adds a new "Content View" which will be the superview of all elements in the cell. To set the background color of the cell, set the background color of this Content View. The solutions above didnt work for me but this one worked well for me.

Solution 13 - Ios

Had a similar problem, had to

  1. Set blue as the selectionStyle and

  2. add this to the code to correct it

     override func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
     
       var bgColorView: UIView = UIView()
       bgColorView.backgroundColor = UIColor(red: (76.0 / 255.0), green: (161.0 / 255.0), blue: (255.0 / 255.0), alpha: 1.0)
       bgColorView.layer.masksToBounds = true
       tableView.cellForRowAtIndexPath(indexPath)!.selectedBackgroundView = bgColorView
       return true
     }
    

Solution 14 - Ios

You can also use colour code in order to make your UITableView Cell lucrative.

[cell setBackgroundColor:[self colorWithHexString:@"1fbbff"]];

This is the code for applying colour code method:

-(UIColor*)colorWithHexString:(NSString*)hex
{
    NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];


    if ([cString length] < 6) return [UIColor grayColor];

    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];

    if ([cString length] != 6) return  [UIColor grayColor];

    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];

    range.location = 2;
    NSString *gString = [cString substringWithRange:range];

    range.location = 4;
    NSString *bString = [cString substringWithRange:range];

    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];

    return [UIColor colorWithRed:((float) r / 255.0f)
                         green:((float) g / 255.0f)
                          blue:((float) b / 255.0f)
                         alpha:1.0f];
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKjulyView Question on Stackoverflow
Solution 1 - IosKjulyView Answer on Stackoverflow
Solution 2 - IosAnton FilimonovView Answer on Stackoverflow
Solution 3 - IossanjanaView Answer on Stackoverflow
Solution 4 - IosYiming TangView Answer on Stackoverflow
Solution 5 - IosEndamaView Answer on Stackoverflow
Solution 6 - Iossuperarts.orgView Answer on Stackoverflow
Solution 7 - IosabboodView Answer on Stackoverflow
Solution 8 - IosMikeView Answer on Stackoverflow
Solution 9 - IosRajVView Answer on Stackoverflow
Solution 10 - IosDan BeaulieuView Answer on Stackoverflow
Solution 11 - IosmllmView Answer on Stackoverflow
Solution 12 - IosDrBugView Answer on Stackoverflow
Solution 13 - IosNakul SudhakarView Answer on Stackoverflow
Solution 14 - IosMahboobiOSDeveloperView Answer on Stackoverflow