UITableViewCell是“预先构建的”,UILabel是您初始化后的唯一子视图。 ID 真 喜欢改变所说标签的背景颜色,但无论我做什么,颜色都不会改变。有问题的代码:
UILabel* label = (UILabel*)[cell.contentView.subviews objectAtIndex:0];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor darkGrayColor];
label.opaque = YES;
你的代码片段对我来说很好,但是必须在将单元格添加到表格并显示之后完成,我相信。如果从中调用 initWithFrame:reuseIdentifier:
,你会得到一个例外,因为 UILabel
子视图 尚未创建。
可能最好的解决方案是添加自己的解决方案 UILabel
,根据您的标准配置,而不是依赖于内置的这条(非常摇摇晃晃的)路径。
你的代码片段对我来说很好,但是必须在将单元格添加到表格并显示之后完成,我相信。如果从中调用 initWithFrame:reuseIdentifier:
,你会得到一个例外,因为 UILabel
子视图 尚未创建。
可能最好的解决方案是添加自己的解决方案 UILabel
,根据您的标准配置,而不是依赖于内置的这条(非常摇摇晃晃的)路径。
这不起作用,因为UITableViewCell在layoutSubviews方法中设置其标签backgroundColors。
如果要更改内置textLabel或detailTextLabel的颜色,请为UITableViewCell创建子类并覆盖layoutSubviews。调用超级实现,然后将backgroundColor属性更改为您想要的。
- (void) layoutSubviews
{
[super layoutSubviews];
self.textLabel.backgroundColor = [UIColor redColor];
}
在分配单元格时,将自己的标签添加到contentView,而不是依赖于内置单元格的提取。然后你可以控制所有值:
UILabel* label = [[[UILabel alloc] init] autorelease];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor darkGrayColor];
label.opaque = YES;
[cell.contentView addSubview:label];
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);
}