Providing color for alternate cells as a table in xcode

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

+3
source share
5 answers

In your data source method, cell:(UITableviewCell*) forRowAtIndexPath:(NSIndexPath*) indexPathpaste this test:

NSUinteger row=indexPath.row;
if (row % 2==0)
 cell.contentView.backGroundColor=[UIColor blueClor] ;
else
 cell.contentView.backGroundColor=[UIColor whiteClor] ;
+4
source

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;
    }
}
+8
source

:

,

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if ((indexPath.row % 2) == 0) {

    cell.backgroundColor = [UIColor whiteColor];

            }
    else {

    cell.backgroundColor = [UIColor lightgrayColor];
        }
}

, !

+2

UITableViewCell.

if(indexPath.row % 2 == 0)
{
    myCell.backgroundColor = [UIColor whiteColor];
}
else if(indexPath.row % 2 == 1)
{
    myCell.backgroundColor = [UIColor grayColor];
}
+2

:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{
cell.backgroundColor = indexPath.row % 2 ? [UIColor lightGrayColor] : [UIColor whiteColor];
}
+2

All Articles