【发布时间】:2012-01-10 15:40:05
【问题描述】:
我需要将表格单元格的背景设置为特定颜色。 (分别为#222222 或 RGB(32,32,32))
IB中表格视图的背景设置正确。正确的灰色出现在表格标题的背面和部分标题等中。 但我与细胞斗争。
要自定义单元格的外观,我将 UITableCell 子类化并实现 layoutSubviews 方法。
这很好用:
- (void)layoutSubviews {
[super layoutSubviews];
self.selectionStyle = UITableViewCellSelectionStyleGray;
self.backgroundColor = [UIColor darkGrayColor];
self.contentView.backgroundColor = [UIColor darkGrayColor];
self.textLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.textColor = [UIColor grayColor];
}
但是,grayColor 和 darkGrayColor 根本不匹配我需要的颜色。
我自然尝试了 UIColor 的 colorWithRed:green:blue:alpha 方法。
- (void)layoutSubviews {
[super layoutSubviews];
self.selectionStyle = UITableViewCellSelectionStyleGray;
self.backgroundColor = [UIColor colorWithRed:(32/256) green:(32/256) blue:(32/256) alpha:1];
self.contentView.backgroundColor = [UIColor colorWithRed:(32/256) green:(32/256) blue:(32/256) alpha:1];
self.textLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.textColor = [UIColor colorWithRed:(32/256) green:(32/256) blue:(32/256) alpha:1];
}
那会导致 detailTextLable 的黑色背景和黑色。 (当然,为背景和文本标签使用相同的颜色是没有意义的。我只是想弄清楚 colorWithRed:green:blue:alpha 做什么和不做什么。)
使用普通风格的桌子我很好。那些单元格根本没有背景颜色。当我只是省略设置 backgroundColor 和 contentView 的背景颜色属性时,单元格的背景显示为在 IB 中定义为表的背景颜色。 但是对于分组表,标准背景是一些浅灰色,我想将其更改为与我客户的风格指南相匹配的更体面的颜色。
我做错了什么?我是否正确使用 colorWithRed:green:blue:alpha ?
非常感谢任何建议。
【问题讨论】:
标签: iphone objective-c ios