Copy NSTableView row greyed out

Hello,


I would like to add copy (Command + C) functionality to my NSTableView for a row (or multiple rows) that the user has selected.


I've tried adding in the NSTableViewController the following things with no luck, the copy function (Command + C) always appears greyed out in the menu:


1.

override func copy() -> AnyObject {

print("TEST: copytriggered") /*Here i would copy to clipboard the selected rows*/

return true

}


2.

Conforming to NSCopying, implementing copyWithZone...


3.

Trying 1 and 2 in the window controller and app delegate.


4.

Adding func tableView(_ tableView: NSTableView, pasteboardWriterForRow row: Int) -> NSPasteboardWriting? in the TableView data source.


I have no clue what to do at this point, any help appreciated.


Thanks,


Marc

Accepted Reply

You are confusing the Copy edit action method with the methods for making a duplicate of an object. The NSCopying protocol and the copyWithZone() method are for the latter, enabling an object to create a copy of itself. Likewise, the copy() method is just a convenience cover method for copyWithZone().


The action method for the Copy edit menu item is subtly different. It's "func copy(_ sender: AnyObject?)". That's the general form for all action methods, taking a sender argument and not returning anything. You need to implement that method.


Finally, tableView(_:pasteboardWriterForRow:) is generally only used for drag-and-drop, when the user drags a row.

Replies

You are confusing the Copy edit action method with the methods for making a duplicate of an object. The NSCopying protocol and the copyWithZone() method are for the latter, enabling an object to create a copy of itself. Likewise, the copy() method is just a convenience cover method for copyWithZone().


The action method for the Copy edit menu item is subtly different. It's "func copy(_ sender: AnyObject?)". That's the general form for all action methods, taking a sender argument and not returning anything. You need to implement that method.


Finally, tableView(_:pasteboardWriterForRow:) is generally only used for drag-and-drop, when the user drags a row.

Yes! This is it, thank you!!!!


Marc