UITableView: Adjust destination index path when dragging and dropping from another view

When reordering items within a table view, I can use tableView:targetIndexPathForMoveFromRowAtIndexPath: to adjust the destination index path.

Is there a way to adjust the destination index path when dropping onto a table view if the drag was initiated in a different view?

For example, if I'm dropping an object into a table view where the rows are sorted alphabetically, I want the gap to appear in the location where the item is going to end up, regardless of which row the dragged item is being dragged / dropped at.

Answered by DTS Engineer in 801052022

@jasonmarr You should look into tableView(_:performDropWith:). The delegate method is the only opportunity for accessing and loading the data representations offered in the drag item. You should be able to use that to update the destination indexPaths.

You'd want to use the method to integrate the dropped content into your table view. In your implementation, iterate over the items property of the coordinator object to retrieve the UIDragItem objects associated with the drag operation. Use the data in those objects to update your table view’s data source and the table view itself.

Adopting Drag and Drop in a Table View sample project covers a similar concept. Please review the sample project as well.

@jasonmarr You should look into tableView(_:performDropWith:). The delegate method is the only opportunity for accessing and loading the data representations offered in the drag item. You should be able to use that to update the destination indexPaths.

You'd want to use the method to integrate the dropped content into your table view. In your implementation, iterate over the items property of the coordinator object to retrieve the UIDragItem objects associated with the drag operation. Use the data in those objects to update your table view’s data source and the table view itself.

Adopting Drag and Drop in a Table View sample project covers a similar concept. Please review the sample project as well.

UITableView: Adjust destination index path when dragging and dropping from another view
 
 
Q