ios - 每5个单元格更改一次单元格背景

标签 ios objective-c uitableview colors

您好,我想每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/

相关文章:

ios - 调用 React Native App 接收来自浏览器的共享信息

ios - avplayer无法播放通过IOS Objective-C中的设备相机捕获的视频中的音频

objective-c - NSSlider 多次撤销注册

ios - 识别长按 UITableViewCell 的真实索引路径

编辑时iOS UITextField未清除

ios - xcode 控制台中的 GKSConnSettings 消息

ios - xcode 4.5 不允许我在 ios 5.1 设备上调试

ios - 如何从设备访问本地主机

objective-c - 如何正确测量 NSInputStream 数据速率

swift - 如何在 UITableViewCell swift 中获取 UIView 的正确宽度