0

I've looked at dozens of answers to this problem on this very site but I still can't get it to work. I, like many before me, want to change the color in a few cells in a table view. I tried putting a conditional if (indexPath.row == ...) then change the color of a few cells to red, but this always results in other cells gradually becoming red changing the more I scroll. I tried moving this code from:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

To here:

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

But this still doesn't work. How can I change the colors of cells, say, 5. 13, and 40 to be red?

rmaddy
  • 314,917
  • 42
  • 532
  • 579

3 Answers3

0

This may be a problem in your cell reuse, that is why you got colours changing when scrolling.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

//Cell creation code goes here, including cell re-usage

    if(indexPath.row % 2 == 0){
        cell.backgroundColor = [UIColor redColor];
    }
    //This else part is IMPORTANT
    else{
        cell.backgroundColor = [UIColor clearColor];
    }
}

You'll get a better understanding about Cell Reuse in UITableView

Community
  • 1
  • 1
Bilal Saifudeen
  • 1,677
  • 14
  • 14
  • Thank you, yes, that is definitely the issue but I'm still not sure how I can resolve it the way I want. I ultimately want 3 groups of colors so I may have to do this in a completely different way. – user3159275 Jan 04 '14 at 04:35
  • You can give any number of groups of colour, also consider the ELSE part – Bilal Saifudeen Jan 04 '14 at 04:39
0

Please try to use this method..

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 5 | indexPath.row == 13 | indexPath.row == 40)
        cell.backgroundColor = [UIColor redColor];
    else 
        cell.backgroundColor = [UIColor clearColor]; 
}
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
  • Maybe I wasn't clear but this is exactly the code I am using that doesn't work. Other cells begin to turn red as I scroll. If I limit it to one cell turning red, exactly 12 cells later another will be red as I scroll. – user3159275 Jan 04 '14 at 04:42
  • Are you sure for reusable cell. I mean are you using reusable cell?. – Dharmbir Singh Jan 04 '14 at 04:43
0

If you large number of value you can take in array and put in a condition like

if([array containsObject:indexPath.row])

Otherwise this is good to give color in cell in willDisplayCell

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 5 | indexPath.row == 13 | indexPath.row == 40)
        cell.backgroundColor = [UIColor redColor];
    else 
        cell.backgroundColor = [UIColor clearColor];
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Rinku
  • 910
  • 13
  • 28