I have an NSCollectionView bound to an NSArrayController. I want to get drag and drop working, so I create a delegate and implement the methods
-(BOOL)collectionView:(NSCollectionView *)collectionView canDragItemsAtIndexes:(NSIndexSet *)indexes withEvent:(NSEvent*)event
-(BOOL)collectionView:(NSCollectionView *)collectionView acceptDrop:(id < NSDraggingInfo >)draggingInfo index:(NSInteger)index dropOperation:(NSCollectionViewDropOperation)dropOperation
-(NSDragOperation)collectionView:(NSCollectionView *)collectionView validateDrop:(id < NSDraggingInfo >)draggingInfo proposedIndex:(NSInteger *)proposedDropIndex dropOperation:(NSCollectionViewDropOperation *)proposedDropOperation
-(NSArray *)collectionView:(NSCollectionView *)collectionView namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropURL forDraggedItemsAtIndexes:(NSIndexSet *)indexes
I'm returning YES for the two BOOL methods, NSDragOperationMove for the validateDrop: method, and an empty array for the namesOfPromisedFilesDroppedAtDestination: method. I also have an NSLog statement as the first line in each method so I can see when they get called.
Right now, the only method that gets called is canDragItemsAtIndexes: (where I return YES). I see that it gets called, but any further dragging just modifies the selection. The rest never get called.
If I make the NSCollectionView not support selections, then not even that method gets called.
I'm sure I'm missing something super obvious, but I can't figure out what it is. Has anyone gotten drag and drop working with NSCollectionViews and can shed some light?
I think you miss the part where you write the drag content to the pasteboard.
To support drag and drop you have to perform the following steps:
YES
, write the content to the PasteboardWriting to the Pasteboard should be implemented in- collectionView:writeItemsAtIndexes:toPasteboard:
You also have to register your dragged types with - registerForDraggedTypes:
Some sample code: http://developer.apple.com/library/mac/#samplecode/IconCollection/Introduction/Intro.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With