在UITableviewCell中交替使用3种颜色

Kyl*_*man 0 background-color uitableview ios

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

到目前为止,这是我在堆栈溢出时能够找到的全部内容:

if (indexPath.row % 2) {
        cell.contentView.backgroundColor = [UIColor lightGrayColor];
    } else {
        cell.contentView.backgroundColor = [UIColor darkGrayColor];
}
Run Code Online (Sandbox Code Playgroud)

这适用于2种颜色.我怎样才能在3种颜色之间交替?

Rob*_*Rob 5

只需使用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;
}
Run Code Online (Sandbox Code Playgroud)

[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0]如果你想要更多灰度(例如50),你也可以使用你想要的任何数值.