I have a little demo on how to use NSOutlineView with drag and drop on my download page: http://davedelong.com/downloads This should be pretty applicable to NSTableView as NSOutlineView is a subclass of NSTableView.
Here is a 5 minute distillation of what I learned after about a month of reading the documentation. There are three βbasicβ NSTableView delegation methods used when dragging items from NSTableViews. It:
- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation;
- (NSDragOperation)tableView:(NSTableView *)aTableView validateDrop:(id < NSDraggingInfo >)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)operation;
- (BOOL)tableView:(NSTableView *)aTableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard;
First, tableView:acceptDrop:row:dropOperation: used when someone is dragging something into your NSTableView. This is pretty straight forward. All information about the reduction (including what is deleted) is in [info draggingPasteboard] . Return YES if the drop was successful, and NO otherwise.
tableView:validateDrop:proposedRow:proposedDropOperation: used when someone WANTS throws something at your NSTableView. The creator of the fall, at the moment, does not matter. All that matters is that they soar above you, and you have to say what they can do. If, for example, they hang over a certain line ("suggested Row"), and you do not want the material to be discarded on that line, return NSDragOperationNone . Or maybe you want to copy what's in the information (return NSDragOperationCopy ). There are a whole bunch of different types of operations that you can identify . Use the one that suits your needs. (These return values ββwill adjust the cursor accordingly. Therefore, if you return NSDragOperationCopy, the cursor will get a small green + circle.)
The last tableView:writeRowsWithIndexes:toPasteboard: called when the user selects several rows in your NSTableView and starts dragging them. Now you need to provide a drag and drop of cardboard with information corresponding to these lines, so you can drop it elsewhere. Here's a simplified example of how I used this method to provide NSManagedObjects to cardboard:
- (BOOL)tableView:(NSTableView *)aTableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard { [pboard declareTypes:[NSArray arrayWithObject:kMyCustomDragType] owner:self];
The idea is that I encode the URIR representation of each selected NSManagedObject object identifier and put it on the file cabinet. I put this data in a draggingPasteboard under the type "kMyCustomDragType" (NSString), which means that only those objects that indicate that they accept drops of type kMyCustomDragType can get this drop. I finally return YES to indicate that I have successfully written data to the file cabinet. (Return NO if a failure occurs)
If you can make this work, then you are likely to get 90% of all the drag and drop features you need. The other 10% will come from weird demands. As always, the documentation will be your best friend .