0

我希望能够创建一个具有例如 4 种颜色的数组。然后在我的 tableview 单元格中使用它们,在 cellForRowAtIndexPath 内。像这样:

-----------------
|      red      |
-----------------
-----------------
|      blue     |
-----------------
-----------------
|     yellow    |
-----------------
-----------------
|     green     |
-----------------
-----------------
|      red      |
-----------------
-----------------
|      blue     |
-----------------
-----------------
|     yellow    |
-----------------
-----------------
|     green     |
-----------------

等等。我可以在 javascript 中使用模数来做到这一点,但在 xcode/obj c 中我无法弄清楚。我到处找。我设法改变每一秒,或者只是完全随机的颜色。

4

3 回答 3

1

It is very simple just find the modulus of total number of colors which you wants to apply in cellForRowAtIndexPath method of UITableView. Which solves your problem. Still you are facing any problem then fell free to ask your problem again.

于 2012-12-13T06:12:46.093 回答
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
     }

这样每个你都会得到每四行重复的颜色,我希望这会有所帮助,祝你好运!

于 2012-12-13T06:01:41.783 回答
0

C 中的模数是 % 运算符,就像在 JavaScript 中一样。您可以将 indexPath.row % number_of_colors 带到颜色索引中。

于 2012-12-13T06:02:35.283 回答