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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With