0

I'm putting custom colors into table cells that can change on circumstances. The code below changes the color back and forth fine. I don't like the fact that have to do a reloadData every time to get the colors to show up. Is there a less expensive way to force a cell color update?

There is other code here that has been stripped out.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // testing for old selected color
    if (checkedIndexPath)
    {
        // returning old checked cell back to blue
        UITableViewCell *uncheckCell = [tableView cellForRowAtIndexPath:checkedIndexPath];
        uncheckCell.backgroundColor = [UIColor blueColor];
    }

    // changing selected cell background color
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
    checkedIndexPath = indexPath;

    // reloadingtable data
    [self.tableVerseView reloadData];
}
rptwsthi
  • 10,094
  • 10
  • 68
  • 109
Grymjack
  • 529
  • 1
  • 10
  • 21
  • 1
    this really isn't that expensive. Can you explain a little more, could you just do custom theming on the rows instead. – John Riselvato Mar 02 '13 at 15:21
  • there are about four different colors that could display based on different circumstances. I was told that reload was expensive, maybe that was incorrect. – Grymjack Mar 02 '13 at 15:27

3 Answers3

3

You don't need the reloadData method. There's the reloadRowsAtIndexPaths method for that :

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [(MyCellClass *)[self.tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor greenColor]];
    [self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows]  withRowAnimation:UITableViewRowAnimationAutomatic];
}
moxy
  • 1,634
  • 1
  • 11
  • 19
  • do you know of a way to change the font color using the method in my code? – Grymjack Mar 02 '13 at 15:27
  • @Grymjack : yes, see the line (it goes on the right more than it is displayed) in my answer beginning by [(MyCellClass *). Basically the code changes the color of the cell and reload visible rows. You can change any property (even label, image or whatever) then reload the row. – moxy Mar 02 '13 at 15:30
  • Why reload all visible cells? Since the colors are only being changed for the selected row, just reload the selected row. And only reload the unselected cell if is it currently visible. This will be far less "expensive" than reloading the table or all of the visible rows. – rmaddy Mar 02 '13 at 15:48
  • it doesn't like the `.self` in there, take those out and it doesn't like the `= [UIColor greenColor]` – Grymjack Mar 02 '13 at 15:49
  • `((NewVersesTableCell *)[tableView cellForRowAtIndexPath:indexPath]).labelVerseTitleCell.textColor = [UIColor blackColor];` that compiled, but the cell just flickered and stayed the same color – Grymjack Mar 02 '13 at 15:58
  • @rmaddy you're right, it's only an expurged copy paste from one of my test files that was doing other stuff, same for the tableView name, he will probably use his (tableVerseView). It's not to take as is. Grymjack will need to adapt it to his own project/needs. – moxy Mar 02 '13 at 16:02
  • @Grymjack : you wanted to change the background color in your code, not the textColor (which must be already black). – moxy Mar 02 '13 at 16:35
  • sorry if I mudded the waters with an additional question about the font color. I will post the font color in a different question. – Grymjack Mar 02 '13 at 16:41
2

I do not think there is any other method to reload all the cells of table other than [... reloadData].

You need to do it as you are doing.

EDIT:

To change the font color use :

label.textColor=[UIColor redColor];
label.font=[UIFont fontWithName:@"Helvetica-Bold" size:12.0];
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • You can also reload individual rows if you really want to , but reloadData is the way to go. – Sean Mar 02 '13 at 15:16
  • do you know of a way to change the font color using the method in my code? – Grymjack Mar 02 '13 at 15:25
  • `UITableViewCell *uncheckCell = [tableView cellForRowAtIndexPath:checkedIndexPath];` doesn't work with your `labelname.textColor=[UIColor redColor];` – Grymjack Mar 02 '13 at 15:50
  • You simply pasted that code there!!! You need to change according to your need, instead of `label` use the object name where you are displaying info... – Anoop Vaidya Mar 02 '13 at 15:53
  • actually I didn't `cell.labelVerseTitleCell.textColor = [UIColor blackColor];` was the full line. Object not found on `labelVerseTitleCell` ...and yes that is the correct name – Grymjack Mar 02 '13 at 16:02
  • Post it as another question, with full code, then others including me can give you good advice, and you will have an option to choose from – Anoop Vaidya Mar 02 '13 at 16:04
  • full error message `Property 'labelVerseTitleCell' not found on object of type 'UITableViewCell *' – Grymjack Mar 02 '13 at 16:04
  • ?? the relevant code won't change much past my initial post. I was just plugging in your suggestions as commented and posting the results. I appreciate you taking the time to try and solve my problem and I'm sorry if I wasted your time. :( – Grymjack Mar 02 '13 at 16:21
0

if want to change color of your perteecular cell on some event, you don't need to reloadData, what you need is just to know indexPath cell. If you can get that on your circumstance follow these steps:

  • Retrive cell by cellForRowAthInedexPath method with

    - (UITableViewCell *) retriveCellForIndexPath : (NSIndexPath *) indexPath in : (UITableView *) tableView{
        return [tableView cellForRowAtIndexPath:indexPath];
    }
    
  • Now update cell in the way you want to

  • Also you can update all UIContent inside the cell, just retrive them, with cell reference, In the case of label,

        [cell.textLabel setTextColor:[UIColor redColor]];
    
rptwsthi
  • 10,094
  • 10
  • 68
  • 109