Grymjack
Grymjack

Reputation: 539

forcing an screen update in a table without a reload data

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];
}

Upvotes: 0

Views: 1743

Answers (3)

rptwsthi
rptwsthi

Reputation: 10172

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]];
    

Upvotes: 0

moxy
moxy

Reputation: 1624

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];
}

Upvotes: 3

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

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];

Upvotes: 2

Related Questions