您好,我想每5个单元格更改一次UITableView Cells的颜色,所以我要为第一个单元格设置绿色,第二个单元格为蓝色,依此类推。然后,当我击中5个单元格时,我希望颜色重新开始。
这是我到目前为止所拥有的:
if(indexPath.row % 5 == 0){
cell.backgroundColor = [UIColor blackColor];
} else if (indexPath.row % 4 == 0) {
cell.backgroundColor = [UIColor redColor];
} else if (indexPath.row % 3 == 0) {
cell.backgroundColor = [UIColor greenColor];
} else if (indexPath.row % 2 == 0) {
cell.backgroundColor = [UIColor blueColor];
} else if(indexPath.row % 1 == 0) {
cell.backgroundColor = [UIColor orangeColor];
如果有人能指出正确的方向,我将不胜感激。谢谢!
最佳答案
我认为这是您要寻找的:
if(indexPath.row % 5 == 0)
{
cell.backgroundColor = [UIColor blackColor];
}
else if (indexPath.row % 5 == 1)
{
cell.backgroundColor = [UIColor redColor];
}
else if (indexPath.row % 5 == 2)
{
cell.backgroundColor = [UIColor greenColor];
}
else if (indexPath.row % 5 == 3)
{
cell.backgroundColor = [UIColor blueColor];
}
else if(indexPath.row % 5 == 4)
{
cell.backgroundColor = [UIColor orangeColor];
}
您想保持模数除数不变-其余的实际上会改变
关于ios - 每5个单元格更改一次单元格背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22277383/