Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow drops only *between* rows in NSTableView?

I have a view-based NSTableView which accepts images dropped in from the finder. I want to be able to insert new images at the specified drop location (left example below) and prevent drops on rows themselves (the right example below).

I have taken a look a delegate methods and NSTableView properties and methods but could not see a way to prevent drop-on-rows. Any ideas?

Screenshot showing the drop-between-rows (left) and drop-on-rows (right) behaviour.

Figure: The cursor has not been rendered in the screenshots. In the left example I am dragging in a file called Pic_3.png and NSTableView responds by drawing a line at the location where the new image will be inserted. In the right example, I have continued to drag and positioned it over the existing row in the table view. I doesn't make any sense to insert on an existing item so I would like to prevent this behaviour.

like image 406
Daniel Farrell Avatar asked Mar 13 '23 08:03

Daniel Farrell


1 Answers

The delegate method

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

handles the dragging behavior. The parameter proposedDropOperation passes the current NSTableView.DropOperation. Return the designated NSDragOperation if operation is .above otherwise .none. For example

if dropOperation == .above {
   info.animatesToDestination = true
   return .move
} else {
   return .none
}

The other DropOperation case .on represents dragging on the row.

like image 135
vadian Avatar answered Mar 24 '23 05:03

vadian