I'm working on implementing drag and drop to rearrange items in my UICollectionView. The issue I'm running into is that my drop delegate's collectionView(_, performDropWith:) method isn't getting called when the drag has multiple items. With single item drags, it works fine. What would cause it to break with multiple items?
I've replicated it in a basic sample app. Below is the drop delegate.
class CollectionDropDelegate: NSObject, UICollectionViewDropDelegate {
let controller: MainViewController
init(controller: MainViewController) {
self.controller = controller
}
func collectionView(_ collectionView: UICollectionView, canHandle session: any UIDropSession) -> Bool {
session.localDragSession != nil
}
func collectionView(_ collectionView: UICollectionView,
dropSessionDidUpdate session: any UIDropSession,
withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
if session.localDragSession != nil {
.init(operation: .move, intent: .insertAtDestinationIndexPath)
}
else {
.init(operation: .cancel)
}
}
func collectionView(_ collectionView: UICollectionView,
performDropWith coordinator: any UICollectionViewDropCoordinator) {
let destinationPath = coordinator.destinationIndexPath ?? IndexPath(item: 0, section: controller.items.count)
for item in coordinator.items {
guard let draggedItem = item.dragItem.localObject as? NumberItem,
let currentIndex = controller.items.firstIndex(of: draggedItem)
else { continue }
controller.items.remove(at: currentIndex)
controller.items.insert(draggedItem, at: destinationPath.item)
coordinator.drop(item.dragItem, toItemAt: destinationPath)
collectionView.moveItem(at: IndexPath(item: currentIndex, section: 0),
to: destinationPath)
}
}
}