iOS: What is the default background color of UITableViewCell
Asked Answered
L

4

6

I want to set background color for labels inside tableViewCell to opaque but I need default background color of uiTableViewCell that Apple provides.

So how can I get this background or what color is it?

Regards

Laevorotatory answered 8/11, 2011 at 13:34 Comment(3)
What color do they have when you do not change the background color? This should be the default.Awed
How about using those labels's superView property and fetching the backgroundColor from that point?Chacon
This also does not work. I get black background. This is probably cos cell's backgroundColor is null...Laevorotatory
L
1

I have found a solution: Different cell bg color depending on iOS version (4.0 to 5.0)

To sum up I just implement method

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

Thanks!

Laevorotatory answered 10/11, 2011 at 7:43 Comment(0)
A
3

To reset the color of a recycled (dequeued) UITableViewCell, this has worked for me in iOS 5, 6, and 7:

cell.backgroundColor = nil;

As others suggested here, I logged the value of that property for a newly created cell. I saw null. That gave me the idea to nil out the property. Seems to be working in all 3 iOS versions.

As I vaguely recall, iOS has changed drastically the way defaults are set for colors in UITableView and UITableViewCell. In some versions parent containers colors were picked up. In some versions, that default color was greyish white, but changed over time. In iOS 7 White is applied regardless of parents. Note: Don't quote me on this paragraph; look it up if you care. The point is that answers telling you to set a specific color are giving you bad advice.

Acetylide answered 5/12, 2013 at 7:17 Comment(0)
P
1

You can log the background color of the cell

NSLog(@"cell background color is %@", cell.backgroundColor);

or the cell's contentView

NSLog(@"cell contentView's background color is %@", cell.contentView.backgroundColor);

You could also just set your label's color directly:

label.backgroundColor = cell.contentView.backgroundColor;
Posit answered 8/11, 2011 at 14:14 Comment(4)
Neither is working!? I get this: "cell background color is (null)" and "cell contentView's background color is (null)". I logged this in cellForRowAtIndexPath method on device and simulator. This is strange. Why is that?Laevorotatory
U, did I mention that I have custom cells "cell = [[TableViewCellController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];". Is this a problem ?Laevorotatory
TableViewCellController? Is a subclass of what?Posit
It's a subclass of UITableViewCell like it should be... I get null for backgroundColor in both uiTableView class and its uiTableViewCell sublcass. It's really strange. Do you know why this happens?Laevorotatory
R
1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UIColor *color = [cell backgroundColor];
    NSLog(@"%@",color);

Result is

2011-11-08 17:09:20.101 test1[5439:207] UIDeviceRGBColorSpace 1 1 1 1

It indicates that default color is Red=1, Green=1, Blue=1, alpha=1. It's a white color.

Note, that UITableViewCell also has contentView property, which contains real visible color.

Reflexive answered 8/11, 2011 at 14:17 Comment(0)
L
1

I have found a solution: Different cell bg color depending on iOS version (4.0 to 5.0)

To sum up I just implement method

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

Thanks!

Laevorotatory answered 10/11, 2011 at 7:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.