使特定的UITableViewCell具有不同的背景颜色

时间:2014-10-17 01:04:57

标签: ios objective-c uitableview

在我的表视图中,我希望前两个项目具有浅绿色背景颜色。我之前研究过这个主题,并且正在使用这些答案中提供的代码,但它并不适用于我。这是我的代码:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
    cell.backgroundColor = [UIColor colorWithRed:144 green:238 blue:144 alpha:1];
}

else if (indexPath.row == 1)
{
    cell.backgroundColor = [UIColor colorWithRed:144 green:238 blue:144 alpha:1];
}
}

我做错了什么?如何才能使这些代码更加高效和有效?

2 个答案:

答案 0 :(得分:3)

尝试

cell.backgroundColor = [UIColor colorWithRed:144.0/255.0 green:238/255.0 blue:144/255.0 alpha:1];

组件的值定义为介于0.0和1.0之间的浮点数

+ (UIColor *)colorWithRed:(CGFloat)red
                    green:(CGFloat)green
                     blue:(CGFloat)blue
                    alpha:(CGFloat)alpha
     

参数

     

红色
      颜色对象的红色组件,指定为值   0.0到1.0。

     

绿色
    颜色对象的绿色组件,指定为值   从0.0到1.0。

     

蓝色
   颜色对象的蓝色分量,指定为值   从0.0到1.0。 alpha颜色对象的不透明度值,   指定为0.0到1.0之间的值。

https://developer.apple.com/library/ios/documentation/uikit/reference/UIColor_Class/index.html#//apple_ref/occ/clm/UIColor/colorWithRed:green:blue:alpha

答案 1 :(得分:0)

你应该改变

[UIColor colorWithRed:144 green:238 blue:144 alpha:1]

[UIColor colorWithRed:144/255.0 green:238/255.0 blue:144/255.0 alpha:1.0]

由于UIColor API显示red/green/blue的参数应指定为值从0.0到1.0

相关问题