Changing UITableViewCell textLabel background color to clear

三世轮回 提交于 2019-11-27 18:48:17

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

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

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?

Nir Levy

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!