Changing UITableViewCell textLabel background color to clear

Sorin Antohi picture Sorin Antohi · Jul 22, 2009 · Viewed 22k times · Source

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

Answer

plug-in picture plug-in · Apr 15, 2010

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