views:

121

answers:

2

I've got a custom UITableViewCell subview that has a button on it which has a background color representing a priority. The user is allowed to drag the cells around in the table, which changes the priority of the object represented by that row. I want to update the color of the button when they're done, so I've implemented this:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    //update data source
    theCell.button.backgroundColor = newColor;
}

This works insofar as the background color of the button changes as soon as I let up on the move control. However, once the cell animates into place, the background color changes back to its previous color. Other values on the cell are changed properly (if, for example, I change the text, it stays changed).

Any thoughts on what's wrong or a workaround?

+1  A: 

As far as I know it's not wise to change anything in the row while any animations are taking place. (I tried finding this in Google but failed to find an official reference).

I've got around this by doing something like:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    [tableView performSelector:@selector(updateRow) withObject:nil afterDelay:0.1f];
}

- (void)updateRow {
    theCell.button.backgroundColor = newColor;
}

I found that this allows the animation to finish and it doesn't throw away the changes to the cell.

rein
Oh ye drive-by negative scorers - could you leave a comment so we can all learn from your wisdom?
rein
I had to use somewhere around 0.5 for the delay to ensure it works, but at least it works.
Ed Marty
A: 

Does a reloadData get called after moveRowAtIndexPath has exited?

My guess is that you're relying on the tablecell to store your data (ie, the button's background color), but that info should be ultimate stored in your data model and since cells are being reused, the bg color doesn't stick for the one you expect it to. Of course the fact that changing the text "sticks" would seem to contradict this theory. Nonetheless, it certainly feels like something along these lines (a combo of needing the table data to reload and making sure your data model is in sync with the request for a table cell at a given indexPath).

wkw
It is stored in the data model. If I move the cell out of view and back in, it shows the correct color.
Ed Marty
Also, reloadData is not being called, no. None of the delegate or datasource methods are called after moveRowAtIndexPath, the UITableViewCell's setHighlighted and setSelected methods are not being called, I can't find any method that's being called after moveRowAtIndexPath. The cell just seems to reset itself after the animation is completed.
Ed Marty