Skip to content

Instantly share code, notes, and snippets.

@aqubi
Last active May 30, 2020 15:39
Show Gist options
  • Save aqubi/e4f14f14db70268bd7e18e90410c3528 to your computer and use it in GitHub Desktop.
Save aqubi/e4f14f14db70268bd7e18e90410c3528 to your computer and use it in GitHub Desktop.
func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
if coordinator.items.count == 0 { return }
let destinationIndexPath = coordinator.destinationIndexPath ?? IndexPath(row: editImages.images.count, section: 0)
let cellImageWidth = tableView.frame.size.width - tableView.layoutMargins.left - tableView.layoutMargins.right
for item in coordinator.items {
let itemProvider = item.dragItem.itemProvider
if !itemProvider.canLoadObject(ofClass: UIImage.self) { continue }
itemProvider.loadObject(ofClass: UIImage.self) {(object, error) in
guard let image = object as? UIImage else { return }
let cellHeight = image.size.height * (cellImageWidth / image.size.width) + 5
let placeholder = UITableViewDropPlaceholder(insertionIndexPath: destinationIndexPath, reuseIdentifier: "Cell", rowHeight: cellHeight)
placeholder.cellUpdateHandler = { cell in
if let imageCell = cell as? SURImageTableViewCell {
imageCell.iconView.image = image
}
}
DispatchQueue.main.async {
let placeholderContext = coordinator.drop(item.dragItem, to: placeholder)
placeholderContext.commitInsertion { insertionIndexPath in
self.insertImage(image, indexPath: insertionIndexPath)
tableView.insertRows(at: [insertionIndexPath], with: .none)
placeholderContext.deletePlaceholder()
}
}
}
}
coordinator.session.progressIndicatorStyle = .none
}
func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal {
if session.localDragSession != nil {
return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
} else {
return UITableViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment