表单元格背景和文本颜色无法准确设置。

时间:2011-12-01 13:40:13

标签: iphone objective-c ios

我需要将表格单元格的背景设置为特定颜色。 (分别为#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根本不匹配我需要的颜色。

当然我尝试了colorWithRed:绿色:蓝色:UIColor的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:绿色:蓝色:alpha有没有。)

使用普通样式表我很好。那些'细胞根本没有背景颜色。当我只省略设置backgroundColor和contentView的背景颜色属性时,单元格的背景显示为IB中表格的背景颜色。 但是对于分组表格,标准背景是浅灰色,我想改变一些更适合我客户的风格指南的颜色。

我做错了什么?我是否正确使用colorWithRed:green:blue:alpha?

非常感谢任何建议。

3 个答案:

答案 0 :(得分:1)

我会尝试其他一些calucaluting颜色浮动的方法:

[UIColor colorWithRed:(32.0f/255.0f) green:(32.0f/255.0f) blue:(32.0f/255.0f) alpha:1.0f];

因为0也包含0到255个值而不是1到256个值。

如果您希望单元格透明,请使用[UIColor clearColor]

答案 1 :(得分:1)

32/256 = 032/256.0 = 0.125

答案 2 :(得分:0)

感谢您指出错误。

但是,这并不能完全回答与普通表格样式和分组表格样式之间的区别有关的问题。

以防有人发现这个问题很有意思:

  1. 分组样式带有背景视图。我把它设置为零。
  2. gropued样式始终将单元格背景颜色设置为某种东西。因此,省略self.backgroundColor = ...语句是不够的。
  3. 最后

    self.backgroundColor            = [UIColor clearColor];
    

    做了这个伎俩。它与我所犯的Objective-c错误没有远程联系。