views:

139

answers:

2

Hi

In my app i'm using a UITableViewController ("grouped style") which in one of its section I want the user to be able to see what he had selected by making this cell colored and other "uncolored".

Doing it by updating all cells' background color and reloading table data, each time user touches a cell (in didSelectRowAtIndexPath:)

Problem is that there is some processing made in the didSelectRowAtIndexPath: so the color doesn't get changed right a way, rather in a bit delay after touch was made. (I gusse the processing is the resone for the tiny delay)

Is there a better way of doing it?

Any help will be appreciated Liron

P.S. I'm new to all of this...

+1  A: 

In order to do this you need to override one of UITableViewCell's methods. You can subclass UITableViewCell and override a method like so:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];

    // Custom drawing code here
}

This gets fired as soon as a touch is made on a table view cell. If you would like to now have any default coloring on the cell make sure to do the following:

cell.selectionStyle = UITableViewCellSelectionStyleNone;
rickharrison
A: 

Hi Rick thanks for the idea, i tried it like this:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section==0) {
    [[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor: [UIColor redColor]];
}
return indexPath;

}

and added this:

- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section==0) {
    [[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor: [UIColor whiteColor]];
}
return indexPath;

}

but still get the same delayed result... :-/

Liron Loop
Any news on this issue?
rickharrison
I just updated my answer to reflect a correct way to do it. Let me know if this helps you.
rickharrison
Hi Rick,sorry for the huge delay.Your answer helped me alot!Thank you!
Liron Loop