I would like to move items inside a NSCollectionView
using its drag and drop delegate methods. I get it working until the item should get dropped. There is no destination indicator (gap) and when releasing the mouse the item is bouncing back. The delegates validateDrop
and acceptDrop
never gets called. The CollectionViewItems
are showing data from custom objects:
func collectionView(_ collectionView: NSCollectionView, canDragItemsAt indexPaths: Set<IndexPath>, with event: NSEvent) -> Bool {
print("canDragItem", indexPaths)
return true
}
func collectionView(_ collectionView: NSCollectionView, writeItemsAt indexPaths: Set<IndexPath>, to pasteboard: NSPasteboard) -> Bool {
let indexData = Data(NSKeyedArchiver.archivedData(withRootObject: indexPaths))
pasteboard.declareTypes(["my_drag_type_id"], owner: self)
pasteboard.setData(indexData, forType: "my_drag_type_id")
print("write data", indexData)
return true
}
func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndex proposedDropIndex: UnsafeMutablePointer<Int>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionViewDropOperation>) -> NSDragOperation {
print("validation")
return NSDragOperation.move
}
func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, index: Int, dropOperation: NSCollectionViewDropOperation) -> Bool {
let pb = NSPasteboard()
let indexData = (pb.data(forType: "my_drag_type_id"))! as Data
let indexPath = (NSKeyedUnarchiver.unarchiveObject(with: indexData)) as! IndexPath
let draggedCell = indexPath.item as Int
print("acceptDrop", draggedCell)
return true
}
What the heck... I think there is something wrong with writing the item-data to be dragged in the pasteboard. Any suggestions.
validateDrop
andacceptDrop
functions don't have correct parameters. The new versions have aproposedIndexPath
andindexPath
parameter. – Willeke