Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 39 additions & 30 deletions ATSDragToReorderTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -1108,37 +1108,46 @@ - (void)shuffleCellsOutOfWayOfDraggedCellIfNeeded {
}
}

if (rowToMoveTo != nil)
{
// Check with the delegate to ensure that this destination row is valid, and get another one if necessary.

if ([self.tableView.delegate respondsToSelector:@selector(tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:)]) {
rowToMoveTo = [self.tableView.delegate tableView:self.tableView targetIndexPathForMoveFromRowAtIndexPath:self.indexPathBelowDraggedCell toProposedIndexPath:rowToMoveTo];
}


/*
If the dragged cell is covering a new row that isn't the one with the blank item, move the blank item to that new row.
*/
if (rowToMoveTo != nil && !(rowToMoveTo.section == self.indexPathBelowDraggedCell.section && rowToMoveTo.row == self.indexPathBelowDraggedCell.row)) {
/*
Tableview's dataSource must update before we ask the tableview to update rows.
*/
[self.tableView.dataSource tableView:self.tableView moveRowAtIndexPath:self.indexPathBelowDraggedCell toIndexPath:rowToMoveTo];

/*
Update the blank index path
*/
NSIndexPath *formerBlankIndexPath = [[self.indexPathBelowDraggedCell retain] autorelease];
self.indexPathBelowDraggedCell = rowToMoveTo;

/*
Then animate the row updates.
*/
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:formerBlankIndexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:self.indexPathBelowDraggedCell] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];

/*
Keep the cell under the dragged cell hidden.
This is a crucial line of code. Otherwise we get all kinds of graphical weirdness
*/
UITableViewCell *cellToHide = [self.tableView cellForRowAtIndexPath:self.indexPathBelowDraggedCell];
cellToHide.hidden = YES;

/*
If the dragged cell is covering a new row that isn't the one with the blank item, move the blank item to that new row.
*/
if (!(rowToMoveTo.section == self.indexPathBelowDraggedCell.section && rowToMoveTo.row == self.indexPathBelowDraggedCell.row)) {
/*
Tableview's dataSource must update before we ask the tableview to update rows.
*/
[self.tableView.dataSource tableView:self.tableView moveRowAtIndexPath:self.indexPathBelowDraggedCell toIndexPath:rowToMoveTo];

/*
Update the blank index path
*/
NSIndexPath *formerBlankIndexPath = [[self.indexPathBelowDraggedCell retain] autorelease];
self.indexPathBelowDraggedCell = rowToMoveTo;

/*
Then animate the row updates.
*/
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:formerBlankIndexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:self.indexPathBelowDraggedCell] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];

/*
Keep the cell under the dragged cell hidden.
This is a crucial line of code. Otherwise we get all kinds of graphical weirdness
*/
UITableViewCell *cellToHide = [self.tableView cellForRowAtIndexPath:self.indexPathBelowDraggedCell];
cellToHide.hidden = YES;
}

}
}

Expand Down