Thursday, August 19, 2010

Transparent Grouped Tableviews

I ran into a problem today with a transparent grouped table view. On the table view, I set opaque to NO and set the background color to clearColor so that an image behind the table would show through:
Screen shot 2010-08-19 at 1.55.43 PM.png
I can't show you the actual app I was working on because I'm under NDA, but I've created a sample project that shows the problem using a really ugly background image. Notice the black around the corners of the group sections:
Screen shot 2010-08-19 at 1.54.21 PM.png

It took me a while to figure out why this was happening, but with a little crowd-source debugging thanks to Twitter, I've narrowed the culprit down. Even though you can clearly see in the first picture that the table view's background color has been set to clearColor in Interface Builder, that setting is either not being respected, or is being changed somewhere. Now, I'm using stock elements here - no custom views at all - so I know I'm not changing it.

My original fix for this was rather involved, but after a process of elimination where I kept commenting out bits of the fix and and seeing what impact it had, I was able to remove all of the code of my fix except for this line of code:

- (void)viewDidLoad
[super viewDidLoad];
self.tableView.backgroundColor = [UIColor clearColor];

Turns out all the other parts were red herrings leftover from my various attempts to fix the problem. So, if you're having this problem, just manually re-set the table view's background color to clearColor and you should be good.

(and yes, I'm going to file a bug report now)


Jeff Kelley said...

It looks like you’ve got this set up correctly, but did you make it a grouped table view in IB? I would think that calling something like:

[tableView setStyle:UITableViewStyleGrouped];

Would, at some point, change the background. Just a hunch though, and I encourage you to file a bug.

Justin Mecham said...

The timing of this post is impeccable. I just ran into this issue last night and had left it to be fixed later. Thanks!

Jeff LaMarche said...


I did, indeed, make it a grouped table in IB, not in code.

It's definitely a bug.

Brainflash said...

A workaround for this bug is to set the table cell's background view color to clearColor. In cellForRowAtIndexPath add the line:

cell.backgroundView.backgroundColor = [UIColor clearColor];

higgis said...

You're a lifesaver, Jeff. I was grappling with this last week and only came up with things that made it worse.

eSommer said...

My solution:

in viewDidLoad:


[myTable setBackgroundColor:[UIColor clearColor]];

UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];

[self.view insertSubview:bg belowSubview:myTable];

[bg release];


SEO Services Consultants said...

Nice information, many thanks to the author. It is incomprehensible to me now, but in general, the usefulness and significance is overwhelming. Thanks again and good luck! Web Design Company

Altynbek Kurmantegin said...

Thanks to author, it help me. Interesting thing, it seems bug, but it is not fixed yet.

Jason said...

Just ran into this exact same problem, thank you so much for the solution.

I find it hard to believe they still haven't fixed this bug yet