如何设置表格单元格文本标签的背景颜色

时间:2011-03-29 17:13:02

标签: iphone cocoa-touch uitableview uikit

以下是我的一些cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath    *)indexPath
{
...


cell.textLabel.textAlignment = UITextAlignmentRight;
cell.contentView.backgroundColor = [UIColor orangeColor];
cell.textLabel.textColor = [UIColor blueColor];
cell.textLabel.backgroundColor = [UIColor orangeColor];
cell.textLabel.text = @"text";


return cell;
}

我的标签的文字颜色将变为蓝色,但其背景颜色保持在Interface Builder中定义的颜色!我该如何定制?

3 个答案:

答案 0 :(得分:1)

好朋友,终于找到了。答案是覆盖willDisplayCell,由于某种原因,它是设置背景颜色的最后一个地方

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.textLabel.backgroundColor = cell.contentView.backgroundColor;
    cell.detailTextLabel.backgroundColor = cell.contentView.backgroundColor;

}

答案 1 :(得分:0)

您是否需要确保正确设置不透明度,可能是透视而不是颜色?

答案 2 :(得分:0)

尝试

cell.textLabel.backgroundColor = [UIColor clearColor];

编辑:检查以下SO帖子,对您有所帮助。

UILabel Setting Transparent Background Color?