ios 更改 UITableViewCell 中 UILabel 的背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/222652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:20:29  来源:igfitidea点击:

Changing the background color of a UILabel within a UITableViewCell

iosobjective-ccocoa-touch

提问by rpj

A UITableViewCell comes "pre-built" with a UILabel as its one and only subview after you've init'ed it. I'd reallylike to change the background color of said label, but no matter what I do the color does not change. The code in question:

UITableViewCell 是“预先构建的”,并在您初始化它之后将 UILabel 作为它的一个也是唯一的子视图。我真的很想更改所述标签的背景颜色,但无论我做什么,颜色都不会改变。有问题的代码:

UILabel* label = (UILabel*)[cell.contentView.subviews objectAtIndex:0];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor darkGrayColor];
label.opaque = YES;

采纳答案by Ben Gottlieb

Your code snippet works fine for me, but it must be done after the cell has been added to the table and shown, I believe. If called from the initWithFrame:reuseIdentifier:, you'll get an exception, as the UILabelsubviewhas not yet been created.

您的代码片段对我来说很好用,但我相信必须在将单元格添加到表格并显示之后才能完成。如果从 调用initWithFrame:reuseIdentifier:,您将收到异常,因为尚未创建UILabel子视图

Probably the best solution is to add your own UILabel, configured to your standards, rather than relying on this (very rickety) path to the built-in one.

可能最好的解决方案是添加您自己的UILabel、根据您的标准配置的 .

回答by TomSwift

This doesn't work because the UITableViewCell sets its label backgroundColors in the layoutSubviews method.

这不起作用,因为 UITableViewCell 在 layoutSubviews 方法中设置了它的标签 backgroundColors。

If you want to change the color of the built-in textLabel or detailTextLabel, subclass the UITableViewCell and override layoutSubviews. Call the super implementation, THEN change the backgroundColor property to what you want.

如果要更改内置 textLabel 或 detailTextLabel 的颜色,请继承 UITableViewCell 并覆盖 layoutSubviews。调用超级实现,然后将 backgroundColor 属性更改为您想要的。

- (void) layoutSubviews
{   
     [super layoutSubviews];

     self.textLabel.backgroundColor = [UIColor redColor];
}

回答by Kendall Helmstetter Gelner

Add your own label to the contentView when you are allocating the cell, rather than relying on the extraction of the built in one. Then you can control all values:

在分配单元格时将自己的标签添加到 contentView 中,而不是依赖于内置的提取。然后您可以控制所有值:

UILabel* label = [[[UILabel alloc] init] autorelease];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor darkGrayColor];
label.opaque = YES;
[cell.contentView addSubview:label];

回答by Manikandan

for (UIView *views in views.subviews)
{
    UILabel* temp = (UILabel*)[views.subviews objectAtIndex:0];
    temp.textColor = [UIColor whiteColor];        
    temp.shadowColor = [UIColor blackColor];
    temp.shadowOffset = CGSizeMake(0.0f, -1.0f);
}