Ios 如何为带有分区的tableview单元格设置备用背景色
我使用下面的代码设置单元格中的背景色Ios 如何为带有分区的tableview单元格设置备用背景色,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我使用下面的代码设置单元格中的背景色 if (indexPath.item % 2) { // odd [cell setBackgroundColor:[UIColor blueColor]]; } else { // even [cell setBackgroundColor:[UIColor whiteColor]]; } 如果部分的行数为偶数,这是正常的,但如果为奇数,则两个相邻的单元格具有相同的颜色(我有几个设置) 我不知道如何纠正这个问题。你也可以用
if (indexPath.item % 2) {
// odd
[cell setBackgroundColor:[UIColor blueColor]];
} else {
// even
[cell setBackgroundColor:[UIColor whiteColor]];
}
如果部分的行数为偶数,这是正常的,但如果为奇数,则两个相邻的单元格具有相同的颜色(我有几个设置)
我不知道如何纠正这个问题。你也可以用这个
static BOOL isEven = YES;
if(isEven) {
[cell setBackgroundColor:[UIColor blueColor]];
}
else {
[cell setBackgroundColor:[UIColor whiteColor]];
}
我甚至=!伊塞文 尝试使用indexPath.row:
if (indexPath.row % 2) {
// odd
[cell setBackgroundColor:[UIColor blueColor]];
} else {
// even
[cell setBackgroundColor:[UIColor whiteColor]];
}
在cellForRowAt中,只需调用一行:
cell.backgroundColor = indexPath.row % 2 == 0 ? .blue : .white
希望获得以下帮助:)(前面几节中的行数之和+第节中的行数)%2测试indexPath。在检查是否即使是表视图,也不能使用
indexPath.item
时,行%2==0似乎对我有用。您的问题是指indexPath.row
?这将导致意外问题<代码>静态变量对所有程序都是静态的。另外,您不能期望对表的每一行依次调用cellForRow:
(即可以在第3行之前对第4行调用cellForRow
)。