Hi, this is a tutorial showing you how to alternate tableViewCell background colors.
First you need to use the method willDisplayCell :
Code:
- (void)tableView: (UITableView *)tableView willDisplayCell: (UITableViewCell *)cell forRowAtIndexPath: (NSIndexPath *)indexPath
Once you have done that you need to make an if statement telling xcode to alternate the first Cell Background color:
Code:
if ( indexPath.row%2 == 0) { //change the "%2" depending on how many cells you want alternating.
UIColor *altCellColor = [UIColor colorWithRed:256/256.0 green:237/256.0 blue:227/256.0 alpha:1.0]; //this can be changed, at the moment it sets the background color to red.
cell.backgroundColor = altCellColor;
}
At the moment we have set every 1st cell background color to red. (i.e 1/3/5/7/9/12 rows will all have their background color set to red.)
Now we will set the other cell background colors to white.
Code:
if ( indexPath.row%2 == 1) {
UIColor *altCellColor2 = [UIColor colorWithRed:1 green:1 blue:1alpha:1.0]; //this can be changed, at the moment it sets the background color to white.
cell.backgroundColor = altCellColor2;
}
There we have it. You may now change the colors in each if statement to whatever you like.
Hope you enjoyed the tutorial.