You’ll be interested in this function from the UITableViewDataSource protocol:
override func tableView(_ tableView: UITableView,
targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath,
toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath
This starts getting called when the user starts dragging an item around to move it. Every time the item is dragged over a new row in the table view it gets called again. If you want to allow the proposed move, you return the value in proposedDestinationIndexPath. If you want to not allow the item to move at all, you return the value in sourceIndexPath. And if you want to have the item move somewhere else instead, you can copy one of the index path arguments & modify it before returning it.