我在这里看过关于条带化UITableView单元格的帖子,但是还没能弄清楚细节。我的代码是:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Setup code omitted
cell.textLabel.text = @"Blah Blah";
cell.detailTextLabel.text = @"Blah blah blah";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if ([indexPath row] % 2 == 1) {
cell.contentView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
cell.textLabel.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.0];
cell.detailTextLabel.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
cell.accessoryView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
}
return cell;
}
我的结果是:
http://img2.pict.com/18/df/b9/2661271/0/screenshot20100127at3.png
为什么会发生这种情况?谢谢!
答案 0 :(得分:4)
这些单元格可以重复使用,在[indexpath row] % 2 == 0
答案 1 :(得分:2)
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setBackgroundColor: indexPath.row % 2 == 0 ? color1 : color2];
}