Skip to content

Instantly share code, notes, and snippets.

@vulgur
Created August 11, 2018 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vulgur/da341c8e530a051a3ccfdfcdedfbc665 to your computer and use it in GitHub Desktop.
Save vulgur/da341c8e530a051a3ccfdfcdedfbc665 to your computer and use it in GitHub Desktop.
[UITableViewDropDelegate] #drop
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