I’m trying to implement drag and drop from the Finder into an NSTableView of my app. The setup uses an NSTableView, an array controller which acts as a datasource using Cocoa bindings to a Core Data store.
I did the following, basically following various blog posts I found on SO and other sites:
In the awakeFromNib of my view controller I call:
[[self sourcesTableView] registerForDraggedTypes:[NSArray arrayWithObjects: NSPasteboardTypePNG, nil]];
I subclassed NSArrayController and added the following methods to my subclass (the reasoning for subclassing is that the array controller needs to be informed of the drop as it acts as the datasource of the table view):
- (BOOL) tableView: (NSTableView *) aTableView acceptDrop: (id < NSDraggingInfo >) info row: (NSInteger) row dropOperation: (NSTableViewDropOperation)operation
My implementation for the above currently only writes to the log and then returns a boolean YES.
- (NSDragOperation) tableView: (NSTableView *) aTableView validateDrop: (id < NSDraggingInfo >) info proposedRow: (NSInteger) row proposedDropOperation: (NSTableViewDropOperation) operation
In IB I have the array controller pointing to my custom NSArrayController subclass.
Result: nothing. When I drag a PNG from the desktop onto my table view, nothing happens and the file happily bounces back to its origin. I must be doing something wrong but don’t understand what. Where am I going wrong?
A drag from the Finder is always a file drag, not an image drag. You’ll need to support the dragging of URLs from the Finder.
To do that, you need to declare that you want URL types:
You can validate the files like so:
You’ll then need to extract the URL again when the
tableView:acceptDrop:row:dropOperation:method is called, create an image from the URL and then do something with that image.Even though you are using Cocoa bindings, you still need to assign and implement an object as the
datasourceof yourNSTableViewif you want to use the dragging methods. SubclassingNSTableViewwill do no good because the datasource methods are not implemented inNSTableView.You only need to implement the dragging-related methods in your datasource object, not the ones that provide table data as you’re using bindings to do that. It’s your responsibility to notify the array controller of the result of the drop, either by calling one of the
NSArrayControllermethods such asinsertObject:atArrangedObjectIndex:or by modifying the backing array using Key-Value Coding-compliant accessor methods.