Question

I have a NSCollectionView, that displays some files: enter image description here

What I want is...

  1. being able to select one file.
  2. being able to drag this file and drop it on the desktop or another Finder window.

The file icons are displayed by a NSImageView.

What would be a good approach to provide such functionality?

Thanks a lot!

Was it helpful?

Solution

You need to call declareTypes:owner: on the collection view and pass in the pasteboard types that you want to be able to drag. In your case you want NSURLPboardType.

All you need to do then is to implement the three dragging-related delegate methods in your NSCollectionView's delegate.

You should return YES from this method:

collectionView:canDragItemsAtIndexes:withEvent:

You can return a custom drag image for each item using this method:

collectionView:draggingImageForItemsAtIndexes:withEvent:offset:

And you write the items to the pasteboard using this method:

collectionView:writeItemsAtIndexes:toPasteboard:

In your case, you would write the URL for the dragged item to the pasteboard in your implementation of this method.

If you don't understand how to work with the pasteboard you should read the guide Drag and Drop Programming Topics.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top