Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag-and-drop files onto an NSTableView?

I have an NSTableView which I wish to allow users to drag-and-drop video files onto. When they drop the file, it'll get added as a row in the table view.

How would I go about doing this? Currently the tableview's takes its data from an Array Controller (which takes its data from a NSMutableArray)

I found this documentation, but cannot seem to make it work..

I have..

  • made a "TableCon" class (which I changed to inherit from NSTableView, not NSObject)
  • changed the NSTableView class to TableCon
  • set the NSTableView's delegate outlet to that class
  • called registerForDraggedTypes in TableCon's init
  • implemented - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender; (again in TableCon)

..but, nothing, it acts like I never changed anything (no errors), what am I doing wrong?

Edit: I've tried implementing Boaz Stuller's suggestion, and also found this description of the solution (the first reply includes the solution to the first post). So what I have done now is..

  • Subclass NSArrayController which feeds content to the table view (TableListCon)
  • Add tableView outlet to TableListCon (and pointed it at the NSTableView)
  • Implement validateDrop, writeRowsWithIndexes, and acceptDrop in TableListCon
  • Called registerForDraggedTypes on the tableView outlet.

Again, no errors/warnings, but only the awakeFromNib method seems to be called (None of the other methods are called)

like image 435
dbr Avatar asked May 04 '09 01:05

dbr


2 Answers

NSTableView handles drag-and-drop differently from generic views, which is overall a good thing. It means that you don't have to manually handle the complicated highlighting, cell tracking and inserting behaviours that tables require.

A description of what is required can be found here. Basically, you still call -registerDraggedTypes: (generally in your -awakeFromNib method) but instead of implementing the NSDraggingDestination methods, you implement the various data source methods associated with drag and drop, which can be found here. You should not need to subclass NSTableView to implement drag-and-drop in this fashion.

Note those are data source methods. You need to hook the table view's dataSource outlet to the class that implements those methods in order for them to be called.

like image 84
Boaz Stuller Avatar answered Sep 28 '22 05:09

Boaz Stuller


In addition to what Boaz said, it sounds like you're creating an NSTableView subclass and then making an instance of that subclass the delegate of NSTableView. If you're going to subclass, that subclass should be used in place of NSTableView, not in addition to it. Also, it's almost always a violation of concerns to have a view be a delegate for another object.

like image 24
Chuck Avatar answered Sep 28 '22 05:09

Chuck