Drag'n'Drop support for NSTableView with CoreData storage

I need NSTableViews in my application to display data that is stored as "CoreData". TableViews are pretty simple, like the whole program.

The right TableView shows the list of checklists, on the left - the contents of this checklist. Now I want to allow the user to drag an item that is in the checklist to another checklist.

I found several guides and sample code on the Internet, but they are a suppression method for my understanding of Cocoa. It would be great if someone could mention a webpage or study guide or the like with short and clear instructions.

thanks a lot

EDIT: Although this should be obvious, my application is written in Cocoa and runs on Mac.

+4
source share
1 answer

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]; //get an array of URIs for the selected objects NSMutableArray * rows = [NSMutableArray array]; NSArray * selectedObjects = [arrayOfManagedObjects objectsAtIndexes:rowIndexes]; for (NSManagedObject * o in selectedObjects) { [rows addObject:[[o objectID] URIRepresentation]]; } NSData * encodedIDs = [NSKeyedArchiver archivedDataWithRootObject:rows]; [pboard setData:encodedIDs forType:kMyCustomDragType]; return YES; } 

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 .

+27
source

All Articles