23

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

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

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

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

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

我也试过:

self.textLabel.backgroundColor = [UIColor redColor];

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

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

谢谢

4

7 回答 7

48

如果你不想继承 UITableViewCell 你可以添加这个:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
     [[cell textLabel] setBackgroundColor:[UIColor clearColor]];
     [[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];
}
于 2010-04-15T07:37:34.980 回答
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];
}
于 2009-08-19T08:38:29.177 回答
3

看起来苹果在这里改变了一些东西。在 iOS4 中这样做是有效的:

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

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

很高兴这是固定的,但如果您使用基本 SDK 4.1 和 min 进行开发,在测试期间会有点令人惊讶。适用于 iPhone 经典版和 iPad 的部署 3.1。

于 2010-10-05T21:07:09.533 回答
0

要设置清晰的背景颜色,您可以使用 clearColor:

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

你是这个意思吗?

于 2009-07-22T10:53:41.873 回答
0

看到这篇文章:如何将 UITableViewCellSelectionStyle 属性设置为一些自定义颜色?

在您的情况下,只需使用具有白色背景颜色的 UIView

于 2009-07-30T13:21:26.487 回答
0

我没有尝试,但这里有一个猜测……为了更快地绘制,标签的 opaque 属性可能默认设置为 true。

尝试

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

如果这也不起作用,我可能会在这一点上放弃并为单元创建自己的 UILabel 。

于 2009-08-08T20:50:41.317 回答
0

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

于 2009-08-13T16:11:32.480 回答