0

I have a document-based Cocoa application with bindings enabled; it's supposed to be a text-snippet manager. I'm developing in system version 10.9 with Xcode 6.2. The main document window has a split view with a NSTableView and a NSTextView. The model objects whose data are displayed in this table we will call Snippets, whose text is displayed in the text view; other data, like creation and modification dates, also show in the table. This array is managed by a subclass of NSArrayController, and the array is in turn an instance variable in another model object (let's call it a Shoebox that holds a collection of Snippets) that can itself be a member an array of such Shoebox objects in the document model.

I created a category on the window controller for this view with the sources for all my pasteboard ops. I have copy, cut, and paste working well. What I do is convert the data of the model object into a property list dictionary (or even an array of such dictionaries) before putting the data on the pasteboard as a Snippet type @"snpt", which is the only pasteboard type I'm declaring in this app.

I implemented tableView: writeRowsWithIndexes: toPasteboard in this pasteboard category file, made my window controller the data source for the table view, and implemented draggingEntered: and performDragOperation:.

In fact, I can pick up a table row from one open document and drag it out of the table it comes from, but I can't drop it in the snippets table for another open document, it just snaps back. With teeth showing.

This drag-and-drop design worked fine for me when I had a document window with a table view as a subview and nothing else on the window. I'm figuring the target window can't figure out what kind of data it's getting, who's sending it, or which view is supposed to receive the drop. Otherwise, I'm stumped at this point.

  • Do you call `registerForDraggedTypes:`? Are the drop methods of `NSTableViewDataSource` implemented? – Willeke Dec 24 '16 at 13:45
  • I knew I'd forget to mention something. That tripped me up once, and that is all it took. As I said, I have the pasteboard code working for a window with a table view and nothing else. Sorry for the omission. Registering is easy to forget, unless it's on a checklist. – daniel julian Dec 24 '16 at 16:22
  • Unless I'm hugely ignorant, I thought all I need is tableView: writeRowsWithIndexes: toPasteboard... – daniel julian Dec 24 '16 at 16:24
  • Is this the one I need...https://developer.apple.com/reference/appkit/nstableviewdatasource/1532052-tableview?language=objc – daniel julian Dec 24 '16 at 16:30
  • And `tableView:acceptDrop:row:dropOperation:`. – Willeke Dec 24 '16 at 16:32
  • OK -- I made a tiny bit of progress. Instead of registering the window for dragged types, I register the table view itself. This seems to be necessary, on top of implementing the two methods @Willeke recommends, above. I just have to figure out the implementation details for these methods, extracting the data I packaged in the property list, converting it to the right kind of object, and adding it to the model array for the drag target. Does that seem about right? – daniel julian Dec 25 '16 at 08:16
  • validateDrop and acceptDrop are getting called, and I thought the pasteboard content should 'just work' at that point; it does with draggingEntered when I just have the one table in my window, but draggingEntered is not getting called here. I've found a few discussions of the latter, but haven't worked out their applicability. – daniel julian Dec 25 '16 at 09:03
  • Well, it turned out not to be hard at all after reviewing the answers in questions/5947091. I copied code from my paste: method into the acceptDrop: method, returning YES for my single pasteboard type and no otherwise. For some reason, I did not believe that I would need to duplicate my paste: code. Please keep this conversation open for at least a few more days, in case I discover mysterious problems I have not tested for yet -- everything seems working OK at this point. Thanks 2 @Willeke! – daniel julian Dec 26 '16 at 18:17

0 Answers0