Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView cell refresh problem on rearrange

Some help would be greatly appreciated here!

I have a custom UITableView cell, consisting of an image (green,orange or red) and two labels in different font sizes. The UITableView is driven by an NSMutableArray of objects.

The UITableView is split into three sections, with the first section containing all the cells with the red image, second section with the orange, third section with the green.

I have implemented the ability to rearrange the cells within UITableView, allowing the user to drag, say, an item with a red image from the red section into the orange section. This dragging works great, and the underlying data is updated correctly. I know this is the case because if I select the cell, I go to its detail view and the data is updated.

However, the UITableView does not update until I go to detail (or exit the app) and return! It also does update if I scroll the offending cell out of view and back into view again, so the array is clearly containing the correct data. I just can't trigger a view refresh!

I have tried the following after my code for changing the underlying data in moveRowAtIndexPath:

[self.tableView reloadData];

However this causes an exception. The Apple Language Reference in fact says that reloadData cannot be called when inserting, deleting or animating, which is of course what I'm doing. In addition, trying just to refresh the cell with

[self setNeedsDisplay];

doesn't work (although it doesn't crash either).

Any ideas on how I can refresh the UITableView? I can't find a afterMoveHasBeenCompleted: method or anything, so if I can't call reloadData during my moveRowAtIndexPath: method then where do I call it?!

like image 352
h4xxr Avatar asked May 08 '09 14:05

h4xxr


2 Answers

Got it!!

I discovered the solution was to put in a delay in the refresh code, which stopped it from crashing.

I put the reloadData call into a separate method:

- (void)refreshDisplay:(UITableView *)tableView {
[tableView reloadData]; }

and then called it with a 0.5 second delay:

[self performSelector:(@selector(refreshDisplay:)) withObject:(tableView) afterDelay:0.5];

And it didn't crash and instead refreshed! The 0.5 sec delay is barely noticeable and totally acceptable.

Hope this helps someone else!

Thanks

Phil

like image 84
h4xxr Avatar answered Sep 21 '22 20:09

h4xxr


Take a look at the reference for UITableViewDelegate. If you assign your UITableView a delegate, you can use its methods to handle notifications that your table view has done something. You will probably use one of

tableView:didEndEditingRowAtIndexPath:
or
tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:
to accomplish this.
like image 40
Tim Avatar answered Sep 21 '22 20:09

Tim