Drag and Drop in an NSTableView

Cocoa, General

Drag and Drop in an NSTableView is easy to do. However, I think the documentation (Table Views: Using Drag and Drop in Tables) for it isn’t particularly great. It misses a few points, so I’m going to go over the basic steps on how to add drag and drop to your TableView. Here, I’ll assume you have a TableView with your source code controller class set as the delegate.

Declare your custom pasteboard format:

#define BasicTableViewDragAndDropDataType @“BasicTableViewDragAndDropDataType”

In awakeFromNib you must register for the drag types you want to receive (you could have others here):

– (void)awakeFromNib {

    [myTableView registerForDraggedTypes:[NSArray arrayWithObjects:BasicTableViewDragAndDropDataType, nil]];

}

Then, you must implement writeRowsWithIndexes to add your data to the pasteboard:

– (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard {

    // Drag and drop support

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];

    [pboard declareTypes:[NSArray arrayWithObject:BasicTableViewDragAndDropDataType] owner:self];

    [pboard setData:data forType:BasicTableViewDragAndDropDataType];

    return YES;

}

Next you will validate the drop:

– (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id )info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)op {

    // Add code here to validate the drop

    NSLog(@“validate Drop”);

    return NSDragOperationEvery;    

}

Finally, you must have a method that accepts the drop. Here you could access the BasicTableViewDragAndDropDataType and look at the rows that were dragged.

– (BOOL)tableView:(NSTableView*)tv acceptDrop:(id )info row:(int)row dropOperation:(NSTableViewDropOperation)op {

    NSLog(@“acceptDrop”);

    // Add code here to accept the drop

    return YES;    

}

And that is it! It is pretty easy…

Technorati Tags:



Subscribe
Notify of
guest

6 Comments
Inline Feedbacks
View all comments
Daniel Jalkut

Thanks! I found it difficult to determine exactly which delegate methods needed to be called when, while first deciphering the “DragNDrop TableView” example. This post makes a concise summary for future reference.

Tristan

Hi Corbin,

Thanks! I have been looking around the web for something like this. It all looks great and looks similar to the “bookmarks” example I found, but my “tableView: validateDrop:…” never gets called, although it’s in the same file as “tableView:writeRowsWithIndexes:…” which gets called fine.

Any suggestions?

Thanks.

[…] of the Documentation for it is fairly complicated. However, I stumbled across this lovely blog post by Corbin, which had these few easy steps (slightly edited here)… Declare your custom […]

laurent

The thing I could not achieved is to drag from a table view to the Finder (like it could create a clipping wih text in the row).

Subscribe to new posts:

You'll get an email whenever a I publish a new post to my blog and nothing more. -- Corbin

As an Amazon Associate I earn from qualifying purchases.

(c) 2008-2024 Corbin Dunn

Privacy Policy

Subscribe to RSS feeds for entries.

70 queries. 0.165 seconds.

Log in