Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when a UITableView is being reordered

I have a UITableView backed by an NSFetchedResultsController which may trigger updates at any time. If the user is currently reordering rows, applying these updates will cause an exception because the table view has temporarily taken over and you get an error like

Invalid update: invalid number of rows in section [...]

How can I detect when the user has started moving a cell so I can delay updates caused by the fetched results controller? There don't seem to be any table view delegate methods to detect this. This delegate method:

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {

Doesn't get called when the user initially detaches the first cell, only when they actually move it somewhere else.

One solution is to check isEditing in the fetched results callbacks and just do a bulk reloadData rather than dynamically inserting/deleting rows, but I'm wondering if there is a way to check specifically for the 'reordering' mode.

like image 644
Mike Weller Avatar asked Nov 26 '22 20:11

Mike Weller


1 Answers

You are wrong, tableView:canMoveRowAtIndexPath does not solve the problem.

Use [cell setEditing:NO] on all cells you want to update or delete.

If the user was dragging a cell, drag mode will be cancelled.

If you need [cell setEditing:YES] you can restore it immediately after finishing whatever you are updating.

like image 56
JohnPayne Avatar answered Dec 24 '22 18:12

JohnPayne