A UITableViewCell comes "pre-built" with a UILabel as its one and only subview after you've init'ed it. I'd really like 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;
4 个解决方案
#1
8
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 UILabel
subview has 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,根据您的标准配置,而不是依赖于内置的(非常摇摇晃晃的)路径。
#2
5
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];
}
#3
2
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];
#4
0
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);
}
#1
8
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 UILabel
subview has 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,根据您的标准配置,而不是依赖于内置的(非常摇摇晃晃的)路径。
#2
5
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];
}
#3
2
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];
#4
0
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);
}