Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color of alternate row in UITableView for iPhone SDK

In my app, I want to display multi-column tableView in iPad.

So, I used a cocoa control from :MultiColumn TableView

it works fine for me, But I want to display the rows in alternate color.

For this, I am not able to find where I change the code for it.

like image 343
user1673099 Avatar asked Mar 19 '13 11:03

user1673099


2 Answers

- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
{
 
        if(indexPath.row % 2 == 0)
              cell.backgroundColor = [UIColor redColor];
        else
              cell.backgroundColor = [UIColor whiteColor];
}
like image 171
Dharmbir Singh Avatar answered Nov 16 '22 04:11

Dharmbir Singh


use the indexPath in the cellForRowAtIndexPath to get the desired results.

    if( [indexPath row] % 2){
          cell.backgroundColor=[UIColor whiteColor];
    } 
    else{
          cell.backgroundColor=[UIColor purpleColor];
    }

You will have this delegate method in the class which is implementing the UITableViewDelegate

like image 9
nsgulliver Avatar answered Nov 16 '22 03:11

nsgulliver