可能重复:
Setting background color of a table view cell on iPhone
how to change the color alternate cell on table view
我希望有一个正常背景的单元格和下一个具有相似背景的单元格,如此截图: 关于如何为无限数量的单元格实现这个漂亮和干净的任何想法? 提前谢谢!
答案 0 :(得分:6)
FizzBuzz:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ((indexPath.row % 2) == 0) {
[cell.contentView setBackgroundColor:[UIColor whiteColor]];
[cell.textLabel setBackgroundColor:[UIColor whiteColor]];
}
else {
[cell.contentView setBackgroundColor:[UIColor colorWithRed:243.0f/255.0f green:243.0f/255.0f blue:243.0f/255.0f alpha:1.0f]];
[cell.textLabel setBackgroundColor:[UIColor colorWithRed:243.0f/255.0f green:243.0f/255.0f blue:243.0f/255.0f alpha:1.0f]];
}
cell.textLabel.text = [songArray objectAtIndex:indexPath.row];
return cell;
}