更改 UITableViewCell textLabel 背景颜色以清除 [英] Changing UITableViewCell textLabel background color to clear

查看:42
本文介绍了更改 UITableViewCell textLabel 背景颜色以清除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个带有 customViewCells 的表格视图.我对 UITableViewCell 类进行了子类化,并添加了一个将异步加载的图像,对于我使用的文本 cell.textLabel.text = @"someThext".

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

对于单元格,背景颜色交替设置为[UIColor darkGrayColor][UIColor whiteColor].

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

当我在模拟器和手机上运行应用程序时,单元格的 textLabel 背景为白色.我想将它设置为清晰,因为我希望单元格的背景颜色是完整的,而不是一条条,然后是白色,然后是另一条.

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.

在我自定义单元格的init方法中添加,希望白色会变成红色,但是没有任何效果:

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

我也试过:

self.textLabel.backgroundColor = [UIColor redColor];

但这也不起作用...如果我添加一个 UILabel 作为子视图,标签的背景颜色可以设置,但我不想这样做,因为当我旋转手机时,我想要我的标签以自动放大.

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.

任何想法为什么设置 cell.textLabel 的背景颜色不起作用?

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

谢谢

推荐答案

问题是 UIKit 在 -setSelected 方法中设置了单元格背景色.我有这个方法,但没有 self.textLabel.backgroundColor = [UIColor clearColor];self.detailTextLabel.backgroundColor = [UIColor clearColor];在里面,所以我添加了它们,图片中提到的问题得到了解决.

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

这篇关于更改 UITableViewCell textLabel 背景颜色以清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆