Drag and Drop in an NSTableView

let tableViewDragType = “MoveRowsType”

override func loadView() {
 messageTableView.register(forDraggedTypes: [tableViewDragType])
}

func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
// Drag and drop support
let data = NSKeyedArchiver.archivedData(withRootObject: rowIndexes)
pboard.declareTypes([tableViewDragType], owner: self)
pboard.setData(data, forType: tableViewDragType)
return true
}

func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int, proposedDropOperation dropOperation: NSTableViewDropOperation) -> NSDragOperation {
// Add code here to validate the drop
print(“validate Drop”)
return NSDragOperation.every
}

func tableView(_ tableView: NSTableView, acceptDrop info: NSDraggingInfo, row: Int, dropOperation: NSTableViewDropOperation) -> Bool {
// Add code here to accept the drop
print(“acceptDrop”)
return true
}

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.