到达数组末尾时如何回到数组的第一行?

How to come back to the first row in an array when you reach the end?

我知道如何更改 UITableViewCell 的背景颜色。但是我有一个可能包含 5-8 种不同颜色的数组,我希望 table 中的单元格遍历数组并按顺序使用每种背景颜色,如果单元格数量大于,则重复该顺序颜色。谢谢!

所以也许您应该更改 post 的标题,因为您的问题是:到达末尾时如何回到数组的第一行。您可以使用模数,它给您除法的其余部分:indexPath.row % yourArray.count:如果它是 8,结果是:

...
7 % 8 = 7
8 % 8 = 0
9 % 8 = 1
etc.

你除以 8,如果你不能除以余下的除法(7 % 8:你不能除以 8,所以你取 7)。 8 % 8 : 可以除以 8,但没有剩余,所以结果为 0。

考虑到您有 colorArray,例如:

NSArray *colorArray = @[[UIColor whiteColor], [UIColor blueColor], [UIColor blackColor], [UIColor purpleColor], [UIColor grayColor], [UIColor greenColor]];

您只需在 UITableViewDelegatetableView:cellForRowAtIndexPath: 方法中将 cell.backgroundColor 设置为 colorArray[indexPath.row%(colorArray.count)]