Search code examples
swiftmacoscocoadrag-and-dropnstableview

NSTableView scrolls too fast when dragging element for reorder


On my macOS project I have a NSTableView with a single column whose rows can be reordered via drag and drop.

When there are too many elements and the scrolling is enabled, the drag and drop can make the table scroll when you approach the bottom or the top. This behavior is out of the box, and I didn't have to enable or configure it.

Issue is that when I start to drag an element and move my mouse up or down just a few pixels, the scrolling movement is really too fast. The table reaches immediately the top or the bottom without the possibility to slow down. It is really frustrating.

I do not understand where to intervene or what to override to customize this behavior. I don't see in the docs anything related to this kind of "automatic" scrolling that I can change. I am really clueless at this point.

A working workaround that was suggested to me is to add a sleep() call in:

func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int, proposedDropOperation dropOperation: NSTableView.DropOperation) -> NSDragOperation

But scrolling becomes a little choppy and I am curios to understand the proper way of doing it.

Thanks in advance for any hint or pointer to a relevant piece of documentation.


Solution

  • It turns out autoscroll speed and hot areas are controlled by the drag and drop mechanism and cannot be overridden explicitly (at least I couldn't find a way).

    BUT this answer saved my day: NSTableView in NSScrollView doesn't autoscroll when dragging

    TL;DR The autoscroll hot areas and scrolling speed depend on NSTableCellView size as set in Interface Builder. A bigger size makes bigger hot areas and faster scrolling.