Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UITableviewCell中的3种颜色之间切换_Ios_Uitableview_Background Color - Fatal编程技术网

Ios 在UITableviewCell中的3种颜色之间切换

Ios 在UITableviewCell中的3种颜色之间切换,ios,uitableview,background-color,Ios,Uitableview,Background Color,我有一个UITableView,我想以3种不同的背景色显示。基本上是白色,浅灰色,深灰色,然后回到白色等 到目前为止,我在堆栈溢出上只能找到以下内容: if (indexPath.row % 2) { cell.contentView.backgroundColor = [UIColor lightGrayColor]; } else { cell.contentView.backgroundColor = [UIColor darkGrayColor];

我有一个UITableView,我想以3种不同的背景色显示。基本上是白色,浅灰色,深灰色,然后回到白色等

到目前为止,我在堆栈溢出上只能找到以下内容:

if (indexPath.row % 2) {
        cell.contentView.backgroundColor = [UIColor lightGrayColor];
    } else {
        cell.contentView.backgroundColor = [UIColor darkGrayColor];
}
这适合两种颜色。如何在3种颜色之间切换?

只需使用3而不是2,然后相应地设置值,例如:

NSInteger colorIndex = indexPath.row % 3;

switch (colorIndex) {
    case 0:
        cell.contentView.backgroundColor = [UIColor whiteColor];
        break;
    case 1:
        cell.contentView.backgroundColor = [UIColor lightGrayColor];
        break;
    case 2:
        cell.contentView.backgroundColor = [UIColor darkGrayColor];
        break;
}
如果您想要更深浅的灰色,例如50,您也可以将[UIColor color with RED:0.5 green:0.5 blue:0.5 alpha:1.0]与您想要的任何数值一起使用。

Rob是正确的,但我将使用UIColor提供的默认颜色:

试试这个:

    if(indexPath.row % 3 ==0){
      cell.contentView.backgroundColor = [UIColor whiteColor];
}
    if((indexPath.row-1) % 3 ==0){
      cell.contentView.backgroundColor = [UIColor lightGrayColor];
}

    if((indexPath.row - 2) % 3 ==0){
      cell.contentView.backgroundColor = [UIColor darkGrayColor];
}

工作很有魅力,你是第一个回答的人。谢谢
    if(indexPath.row % 3 ==0){
      cell.contentView.backgroundColor = [UIColor whiteColor];
}
    if((indexPath.row-1) % 3 ==0){
      cell.contentView.backgroundColor = [UIColor lightGrayColor];
}

    if((indexPath.row - 2) % 3 ==0){
      cell.contentView.backgroundColor = [UIColor darkGrayColor];
}