问题是,如果单元格在第 1 节(或 2、3、...)中,并且前一节有奇数个单元格,则它以与前一个单元格的最后一个单元格相同的颜色结束。
因此,您需要检查上一节的单元格数,如果该数字是奇数,请反转您的“if”语句,如果它是偶数则保持原样
编辑:
int toBeAdded = 0;
if (indexPath.section>0) {
toBeAdded = ([self tableView:tableView numberOfRowsInSection:(indexPath.section -1)]) %2 ;
}
if (([indexpath row]+toBeAdded)%2) {
trackCell.contentView.backgroundColor = [UIColor clearColor];
} else {
trackCell.contentView.backgroundColor = [UIColor colorWithRed:212/255.0 green:212/255.0 blue:212/255.0 alpha:0.1];
}
编辑2:
看到“Will Jenkins”...他是对的...您需要查看所有前面的部分,而不仅仅是前一个...
他计算一个部分中所有单元格的方法不如我的好和快,但他的答案是正确的,+1 对他来说......无论如何,最好的方法可能是将他的循环和我对 tableView 的调用结合起来: tableView numberOfRowsInSection: