Question

In my app I have a table view with customViewCells. I subclassed the UITableViewCell class and added an image that will load async and for the text I use cell.textLabel.text = @"someThext".

For the cells the background color is set alternatively to [UIColor darkGrayColor] and [UIColor whiteColor].

When I run the app in the simulator and on the phone the textLabel of the cell has the background white. I want to set it to be clear, because I want the background color of the cell to be full not a strip then white then another strip.

In the init method of my custom cell I added, hoping that the white will turn into red, but it doesn't have any effect:

[self.textLabel setBackgroundColor:[UIColor redColor]];

I tried also:

self.textLabel.backgroundColor = [UIColor redColor];

But this also didn't work... if I add a UILabel as a subview, the background color of the label can be set, but I don't want to do that because when I rotate the phone I want my labels to auto enlarge.

Any ideas why setting the background color of cell.textLabel doesn't work?

Thank you

Was it helpful?

Solution 2

The problem is that UIKit sets the cell background color in the -setSelected method. I had the method but didn't have self.textLabel.backgroundColor = [UIColor clearColor]; self.detailTextLabel.backgroundColor = [UIColor clearColor]; in it so I added them and the problem mentioned in the picture was fixed.

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    self.textLabel.backgroundColor = [UIColor clearColor];
    self.detailTextLabel.backgroundColor = [UIColor clearColor];
}

OTHER TIPS

If you do not want to subclass UITableViewCell you can just add this:

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

Looks like Apple changed something here. Doing exactly this in iOS4 works:

self.textLabel.backgroundColor = [UIColor xxxColor];
self.detailTextLabel.backgroundColor = [UIColor xxxColor];

At least up to the point that the label background is transparent or takes the background color. Still not possible to set an own background color.

Nice that this is fixed, but a bit surprising during tests if you develop with base SDK 4.1 and min. deployment 3.1 for iPhone classic and iPad.

To set a clear background color you can use clearColor:

    [[cell textLabel] setBackgroundColor:[UIColor clearColor]];

Is this what you mean?

see this post: How do I set UITableViewCellSelectionStyle property to some custom color?

in your case just use a UIView with white background color

I didn't try it but here's a guess... In order to draw faster the opaque property of the label might be set to true by default.

Try

cell.titleLabel.opaque = NO;
cell.titleLabel.backgroundColor = [UIColor clearColor];

If that doesn't work either I would probably just give up at this point and create my own UILabel for the cell.

to set the color of the textlabel you should do this: t.textColor = [UIColor colorYouWantRGB];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top