Tuesday, April 2, 2019

Using Drag and Drop with NSTableView

Nate Thompson (via Daniel Jalkut):

I recently tried to implement drag and drop with NSTableView in a project and ran into a bunch of issues with finding resources that actually helped. Such is the life of a Cocoa developer. So here’s my attempt at the definitive guide to drag and drop with NSTableView. (This should also apply to NSOutlineView, since they work in a similar way.)

[…]

There’s a bug in NSTableView that requires implementing tableView(_:heightOfRow:) to get the gap style to animate correctly.

[…]

Passing -1 and on will highlight the entire table view.

Unfortunately, I have not found that highlighting to be reliable since macOS 10.14.

6 Comments RSS · Twitter

I'm afraid this guide suffers from the usual issue: the original location of a drag is still proposed as an internal drop location. Which is completely useless for a reorder action.

e.g. Xcode sourcelist.

What's the issue with highlighting the table view? I haven't run into any issues with it.

@someone: Could you elaborate? Not sure what you mean by that. Is there something I should address in my post?

@Nate I just mean that one of my table views no longer highlights (the entire table, or anything) when dropping.

@Michael Huh that's odd. If it's anything like my experience with NSTableView, it's something specific, seemingly unrelated, and completely undocumented that you're doing (or not doing) that breaks it somehow.

@Nate Unless I'm mistaken, the video for reordering has been changed since. Because what I saw was the insertion line being displayed just above and under the original position of the item. Which would not reorder the item. These destinations only make sense when internally drag and dropping a multiple discontinuous selection.

@someone Ah, yeah I had just updated the code when this was posted but I don't think the videos had been updated yet. When I originally wrote the tutorial, I didn't know that you could animate a gap between rows of a dragging destination. It's definitely a lot better than using the insertion line.

Leave a Comment