更改UITableViewCell textLabel背景颜色以清除

Sor*_*ohi 23 background-color uitableview iphone-sdk-3.0

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

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

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

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

[self.textLabel setBackgroundColor:[UIColor redColor]];
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

self.textLabel.backgroundColor = [UIColor redColor];
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢

小智 48

如果您不想继承UITableViewCell,可以添加以下内容:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
     [[cell textLabel] setBackgroundColor:[UIColor clearColor]];
     [[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];
}
Run Code Online (Sandbox Code Playgroud)


Sor*_*ohi 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];
}
Run Code Online (Sandbox Code Playgroud)