views:

6064

answers:

9

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 whitecolor.

When i run the app in the simulator and on the pone 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

A: 

To set a clear background color you can use clearColor:

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

Is this what you mean?

Benji Barash
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?
SorinA.
i put there [UIColor redColor] to see if the property is set..but it's ignored, it stays white...
SorinA.
A: 

see this post: http://stackoverflow.com/questions/260523/how-do-i-set-uitableviewcellselectionstyle-property-to-some-custom-color

in your case just use a UIView with white background color

Nir Levy
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..
SorinA.
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...
SorinA.
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
A: 

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
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
SorinA.
A: 

Hi, 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.
SorinA.
+12  A: 

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];
}
SorinA.
A: 

Thanks Sorin for providing that answer i have been looking for a while... :)

Btw, take care of transparent background, it is resource consuming and not recommended! (but so much nice looking... :)

puy0
A: 

thx, guys!! This is a good solution:

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

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
+2  A: 

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