2009-07-22 74 views
23

在我的应用程序中,我有一个带有customViewCells的表格视图。我继承了UITableViewCell类,并添加了一个将加载异步的图像,以及我使用的文本cell.textLabel.text = @"someThext"更改UITableViewCell textLabel背景色以清除

对于单元格,背景颜色可选地设置为[UIColor darkGrayColor][UIColor whiteColor]

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

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

[self.textLabel setBackgroundColor:[UIColor redColor]]; 

我也试过:

self.textLabel.backgroundColor = [UIColor redColor]; 

但是,这也没有奏效......如果我添加一个UILabel作为子视图,可以设置标签的背景颜色,但我不想这样做,因为当我旋转手机时,我希望我的标签能够自动放大。

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

谢谢

回答

23

问题是UIKit在-setSelected方法中设置了单元背景颜色。我有方法,但没有self.textLabel.backgroundColor = [UIColor clearColor]; self.detailTextLabel.backgroundColor = [UIColor clearColor];所以我添加了它们,并且图片中提到的问题已修复。

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

制定明确的背景颜色,您可以使用clearColor:

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

这是什么意思?

+0

我测试过,它不工作... 这里是我的问题的截图链接.. http://img269.imageshack.us/img269/9001/picture1wim.png。 标签不是由我制作的,是由UITableCell提供的。任何想法为什么setBackgroundColor不起作用? – 2009-07-22 12:05:36

+2

我把那里[UIColor redColor]看看属性是否设置..但它被忽略,它保持白色... – 2009-07-22 12:32:38

0

看到这个帖子:How do I set UITableViewCellSelectionStyle property to some custom color?

在你的情况下,只需使用一个UIView与白色背景的彩色

+0

我认为你不明白我的问题......问题不是细胞的背景颜色..我可以将它设置为任何我想要的..我的问题是,我不能设置单元格textLabel的背景颜色。如果我把[[cell textLabel] setBackgroundColor:[UIColor redColor]];该标签的背景保持白色。您之前提到的帖子是指在选择它时更改单元格的背景颜色,因为我想要默认的蓝色.. – 2009-08-03 07:38:33

+0

如果我将UILabel添加为子视图,则不会忽略背景颜色。对于单元格的内置textLabel和detailTextLabel,我为背​​景颜色设置的属性将被忽略... – 2009-08-03 07:45:16

+0

SorinA,除了“willDisplayCell”之外,不能更改textLabel以及任何UITableViewDataSource协议方法中的detailTextLabel的背景颜色,方法。请参阅我以前的帖子,在那里你可以找到解决方案http://stackoverflow.com/questions/1164459/changing-uitableviewcell-textlabel-background-color-to-clear/2643451#2643451 – 2010-06-21 08:38:40

0

我没有尝试,但这里有一个猜测......为了绘制更快的不透明财产该标签默认情况下可能会设置为true。

尝试

cell.titleLabel.opaque = NO; 
cell.titleLabel.backgroundColor = [UIColor clearColor]; 

如果不工作,要么我可能会干脆放弃了在这一点上,并创建自己的UILabel的细胞。

+1

在操作系统3.0的细胞doesn' t回应titleLabel。我试过你用cell.textLabel说过但效果是一样的。谢谢 – 2009-08-11 08:17:05

0

设置文本标签的颜色,你应该这样做: t.textColor = [UIColor colorYouWantRGB];

+0

你好Amelie,我想设置单元格标题标签的背景颜色清除,而不是设置标签的文本颜色。 – 2009-08-13 19:22:13

47

如果你不想子类的UITableViewCell你可以补充一点:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    [[cell textLabel] setBackgroundColor:[UIColor clearColor]]; 
    [[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]]; 
} 
+0

感谢,我疯了,试图让它在3.x手机上工作! – jv42 2010-11-05 14:37:40

3

看起来像苹果改变了这里的东西。这样做在iOS4中的工作原理如下:

self.textLabel.backgroundColor = [UIColor xxxColor]; 
self.detailTextLabel.backgroundColor = [UIColor xxxColor]; 

至少达到标签背景透明或采用背景色的程度。仍然无法设置自己的背景颜色。

不错,这是固定的,但在测试过程中有点令人惊讶,如果你使用基本的SDK 4.1和最小开发。部署3.1 for iPhone classic和iPad。