I want to give alternative colors to my table view cell. for example, the color of the first cell remains white, while the next cell is light gray, then the third is white again, then the next is again light gray, etc. Can someone tell me a way to do this, please.
Thanks Christy
In your data source method, cell:(UITableviewCell*) forRowAtIndexPath:(NSIndexPath*) indexPathpaste this test:
cell:(UITableviewCell*) forRowAtIndexPath:(NSIndexPath*) indexPath
NSUinteger row=indexPath.row; if (row % 2==0) cell.contentView.backGroundColor=[UIColor blueClor] ; else cell.contentView.backGroundColor=[UIColor whiteClor] ;
use this baptism:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0 || indexPath.row%2 == 0) { UIColor *altCellColor = [UIColor colorWithRed:100 green:10 blue:10 alpha:1]; cell.backgroundColor = altCellColor; } }
:
,
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ((indexPath.row % 2) == 0) { cell.backgroundColor = [UIColor whiteColor]; } else { cell.backgroundColor = [UIColor lightgrayColor]; } }
, !
UITableViewCell.
UITableViewCell
if(indexPath.row % 2 == 0) { myCell.backgroundColor = [UIColor whiteColor]; } else if(indexPath.row % 2 == 1) { myCell.backgroundColor = [UIColor grayColor]; }
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { cell.backgroundColor = indexPath.row % 2 ? [UIColor lightGrayColor] : [UIColor whiteColor]; }