你好,我想改变我的UITableView细胞的颜色,以前的5单元,所以我将有绿色的单元格1蓝色的单元格2等等。然后,一旦我击中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];如果有人能指出正确的方向,我会非常感激的。谢谢!
发布于 2014-03-09 02:24:12
我想这就是你要找的:
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];
}你想要保持模数除数不变--它的剩余部分实际上是要改变的。
发布于 2014-03-09 02:27:34
您需要针对相同的数字,而不是不同的数字模块。这应该会为你指明正确的方向:
static NSArray* rowColors;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
rowColors = @[[UIColor redColor], [UIColor blueColor], [UIColor greenColor], [UIColor orangeColor], [UIColor yellowColor]];
});
int rowMod = indexPath.row % rowColors.count;
UIColor* color = rowColors[rowMod];
cell.contentView.backgroundColor = color;这种方法比目前的方法做得更好:
希望能帮上忙!
https://stackoverflow.com/questions/22277383
复制相似问题