NSTableView: Implementing -pasteboardWriterForRow Prevents TextField Selection

Viewed 26

Context

I'm building a Mac app using Xcode 14 RC, Swift 5.7, with a base SDK of macOS 12 and a deployment target of macOS 11.0

In this app, I have a view-based NSTableView. The tableView is backed by an array of Foo objects, which look like this:

final class Foo: NSObject
{
    var uuid: UUID
    var title: String
}

The NSTextField of each NSTableCellView is set to display the value of Foo.title. These textFields have isSelectable = true set and I can see in the debugger that that value is correctly set to true.

NB: "selectable" != "editable". These textFields are SELECTABLE but not EDITABLE. That's important.

The Problem

If I implement this method, the textFields in the table are no longer selectable; you cannot copy their text.

func tableView(_ tableView: NSTableView, pasteboardWriterForRow row: Int) -> NSPasteboardWriting?
{
    return arrayOfFoos[row].uuid.uuidString as? NSString
}

(Each row in the table is draggable and this method lets me pass the UUID so I can easily retrieve it during the ensuing drop sequence.)

Importantly: It does not matter WHAT I return from this method. The textfields are suddenly non-selectable as long as this method is implemented at all, even if it returns nil. However, if I make the textFields editable instead, they will indeed begin editing when clicked. It's only the selectable state that vanishes.

The Question

How do I retain selectable NSTextFields in an NSTableView whose datasource implements -tableView:pasteboardWriterForRow:?

0 Answers
Related