2
votes

I have a simple 1 section NSCollectionView and become crazy about not getting the drag and drop working. The collection view has custom collection view item with labels showing the data of custom objects. everything works until the point where the dragged item should get dropped between the new position. But when I drag the item there is no drop indicator (usually a gap) at the drop destination. I found out that the following NSCollectionView delegate is not getting called.

func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo,
                   proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>,
                   dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionViewDropOperation>) -> NSDragOperation {

    print("validateDrop", proposedDropIndexPath) // never shows up in the console

    if proposedDropOperation.pointee == NSCollectionViewDropOperation.on {
        proposedDropOperation.pointee = NSCollectionViewDropOperation.before
    }
        print("validateDrop", proposedDropIndexPath)
        return NSDragOperation.move
    }

What do I miss??? Thanks!

Edit 1

I only want to use drag and drop to allow reordering the items within the collection view. It is not intended to drag external content in the collection view.

2

2 Answers

1
votes

You should register appropriate dragged type which matching return of collectionView(_:, pasteboardWriterForItemAt:)

for example, if we want to add a NSImage to pasteboard, we should register tiff type for collection view.

self.collectionView.registerForDraggedTypes([.tiff])
func collectionView(_ collectionView: NSCollectionView,
                         indexPath: IndexPath) -> NSPasteboardWriting? {
        SnsDebug("collectionView pasteboardWriterForItemAt")
        let image = self.viewModel.images.value[indexPath.item]
        return image as NSPasteboardWriting? // NSImage
    }
0
votes

Did you register the collectionview for dragged types?

  collectionView.registerForDraggedTypes([NSPasteboard.PasteboardType])