Created
August 11, 2018 14:23
-
-
Save vulgur/da341c8e530a051a3ccfdfcdedfbc665 to your computer and use it in GitHub Desktop.
[UITableViewDropDelegate] #drop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension ShoppingListViewController: UITableViewDropDelegate { | |
func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) { | |
guard let destinationIndexPath = coordinator.destinationIndexPath else { | |
return | |
} | |
DispatchQueue.main.async { [weak self] in | |
tableView.beginUpdates() | |
coordinator.items.forEach({ (item) in | |
guard let sourceIndexPath = item.sourceIndexPath, | |
let `self` = self | |
else {return} | |
let row = self.shoppingList.remove(at: sourceIndexPath.row) | |
self.shoppingList.insert(row, at: destinationIndexPath.row) | |
tableView.moveRow(at: sourceIndexPath, to: destinationIndexPath) | |
}) | |
tableView.endUpdates() | |
} | |
} | |
func tableView(_ tableView: UITableView, canHandle session: UIDropSession) -> Bool { | |
return session.canLoadObjects(ofClass: ListItem.self) | |
} | |
func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal { | |
return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment