Striping Rows of a UITableView
I've read posts here on SO about striping a UITableView's cells, but have not been able to work out the details. My code is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Setup code omitted
cell.textLabel.text = @"Blah Blah";
cell.detailTextLabel.text = @"Blah blah blah";
cell.accessoryType = UITableViewCellAccessoryDisclosu开发者_C百科reIndicator;
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;
}
And my result is:
http://img2.pict.com/18/df/b9/2661271/0/screenshot20100127at3.png
Any ideas why this is happening? Thanks!
These cells get reused, you should set it back to normal in the case of [indexpath row] % 2 == 0
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[cell setBackgroundColor: indexPath.row % 2 == 0 ? color1 : color2];
}
精彩评论