Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSCollectionView Drag and Dropping: Most Delegate Events Not Getting Called

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?

like image 442
James Williams Avatar asked Apr 28 '11 22:04

James Williams


1 Answers

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:

  1. Determine if you can drag in your drag source
  2. If YES, write the content to the Pasteboard
  3. Validate & accept the items in your drop target

Writing 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

like image 152
Thomas Zoechling Avatar answered Oct 21 '22 09:10

Thomas Zoechling