Moving items with UITableViewDiffableDataSource

How does one move items in a UITableView when using UITableViewDiffableDataSource? This used to be by overriding


tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)


But this is a UITableViewDataSource method.

Replies

Move the data in your NSDiffableDataSource and apply the new snapshot to your UITableViewDiffableDataSource.


To move use:

func moveItem(_ identifier: ItemIdentifierType, afterItem toIdentifier: ItemIdentifierType)


Actually what I meant is that when using UITableViewDiffableDataSource, you can no longer get the move action when the table view is in edit mode (drag and drop with the drag handle icon appearing).


It seems like the only way to do this is to implement drag and drop yourself, which is stupid since the whole point of UITableViewDiffableDataSource is to delete code and make things more reliable, not to have to add more code to implement what came for free with UITableViewDataSource.

I hope i dont' need to rewrite all with drag and drop...when before ios13 i got averything for free (animation + behaviour)

You can subclass `UITableViewDiffableDataSource` and override the UITableViewDataSource methods as necessary.


class EditTableViewDataSource: UITableViewDiffableDataSource<Int, Int> {
    
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        return true
    }
}