我希望能够创建一个包含4种颜色的数组。然后在我的tableview单元格中使用它们,在cellForRowAtIndexPath中。像这样:
-----------------
| red |
-----------------
-----------------
| blue |
-----------------
-----------------
| yellow |
-----------------
-----------------
| green |
-----------------
-----------------
| red |
-----------------
-----------------
| blue |
-----------------
-----------------
| yellow |
-----------------
-----------------
| green |
-----------------
等等。我能够在javascript中使用模数来做到这一点,但在xcode / obj c中,我无法弄明白。我到处看。我设法改变每一秒,或者只是改变随机颜色。
答案 0 :(得分:1)
只需在cellForRowAtIndexPath
UITableView
方法中找到要应用的颜色总数模数即可。这解决了你的问题。
你仍然面临任何问题,然后再次自由地问你的问题。
答案 1 :(得分:0)
您可以执行以下操作:
if(indexPath.row %4 ==0)
{
cellView.backgroundColor = [UIColor redColor];
}
else if(indexPath.row %4 ==1)
{
//setBackground to be the second color
}
else if(indexPath.row %4 ==2)
{
//setBackground to be the third color
}
else
{
//setBackground to be the fourth color
}
这样每个你都会得到每四行重复的颜色, 我希望这有帮助,祝你好运!
答案 2 :(得分:0)
C中的模数是%运算符,就像在JavaScript中一样。您可以将indexPath.row%number_of_colors带到颜色索引。