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

查看:90
本文介绍了更改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天全站免登陆