简体   繁体   English

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

[英]How to set the background color of a table cell text label

Here is some of my cellForRowAtIndexPath 这是我的一些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;
}

The text color of my label will change to blue but its background color stays as it's defined in Interface Builder! 标签的文本颜色将变为蓝色,但是其背景颜色保持不变,如在Interface Builder中定义的一样! How can I customize it? 如何自定义?

ok friends, finally found it. 好的朋友,终于找到了。 the answer is to override willDisplayCell, which for some reason, is the last place where the background color is set 答案是要覆盖willDisplayCell,由于某种原因,它将是设置背景色的最后一个位置

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

}

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

try with 尝试

cell.textLabel.backgroundColor = [UIColor clearColor];

Edit : check below SO post, could be helpful for you. 编辑:在SO帖子下面检查,可能对您有帮助。

UILabel Setting Transparent Background Color? UILabel设置透明背景颜色?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM