23

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

Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88
Sorin Antohi
  • 6,145
  • 9
  • 45
  • 71

7 Answers7

48

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]];
}
plug-in
  • 521
  • 1
  • 4
  • 3
23

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];
}
Redwood
  • 66,744
  • 41
  • 126
  • 187
Sorin Antohi
  • 6,145
  • 9
  • 45
  • 71
3

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.

Gerd
  • 328
  • 1
  • 2
  • 9
0

To set a clear background color you can use clearColor:

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

Is this what you mean?

Benji Barash
  • 423
  • 3
  • 7
  • 15
  • i tested that already and it doesn't work... here is a link with a screenshot of my problem.. http://img269.imageshack.us/img269/9001/picture1wim.png . The label is not made by me is the one in the offered by the UITableCell. any ideas why setBackgroundColor would not work? – Sorin Antohi Jul 22 '09 at 12:05
  • 3
    i put there [UIColor redColor] to see if the property is set..but it's ignored, it stays white... – Sorin Antohi Jul 22 '09 at 12:32
0

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

in your case just use a UIView with white background color

Community
  • 1
  • 1
Nir Levy
  • 4,613
  • 2
  • 34
  • 47
  • i think you didn't understand my problem... the problem is not the background color of the cell.. that i can set it to whatever i want.. my problem is that i can't set the background color of the cell's textLabel. If i put [[cell textLabel] setBackgroundColor:[UIColor redColor]]; the background of that label stays white. the post that you refered earlier refers to changing the background color of the cell when selecting it, for that i want the default blue.. – Sorin Antohi Aug 03 '09 at 07:38
  • if i add a UILabel as a subview, the background color is not ignored. For the built in textLabel and detailTextLabel of the cell the proprieties i set for the background color are ignored... – Sorin Antohi Aug 03 '09 at 07:45
  • SorinA, you can't change background color of textLabel as well as detailTextLabel in any of UITableViewDataSource Protocol methods excepts of "willDisplayCell" method. Please refer to my previous post, there you can find the solution http://stackoverflow.com/questions/1164459/changing-uitableviewcell-textlabel-background-color-to-clear/2643451#2643451 – plug-in Jun 21 '10 at 08:38
0

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.

Ron Srebro
  • 6,663
  • 3
  • 27
  • 40
  • 1
    in OS 3.0 the cell doesn't respond to titleLabel. I tried what you've said with cell.textLabel but the effect is the same.. thank you – Sorin Antohi Aug 11 '09 at 08:17
0

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

  • Hello Amelie, i want to set the background color of the cell's title label to clear, not to set the text color of the label. – Sorin Antohi Aug 13 '09 at 19:22