Search code examples
swiftmacosnstableviewnsoutlineview

How do I get the "item" or the row index of an edited NSOutlineView text field cell?


I'm using NSOutlineView with a Text Field / Text Field Cell as show in the images below:

enter image description here

enter image description here

The code for the sent action function:

@IBAction func handleTextFieldDidEndEditing(_ sender: Any) {
    print("LeftNavBarOutlineView selectedRow: \(self.selectedRow)")
    print("LeftNavBarOutlineView editedRow: \(self.editedRow)")
    print("LeftNavBarOutlineView selectedRowIndexes.count: \(self.selectedRowIndexes.count)")

    let taskName = (sender as! NSTextField).stringValue
    print("LeftNavBarOutlineView " + taskName)
}

The printout result:

LeftNavBarOutlineView selectedRow: -1
LeftNavBarOutlineView editedRow: -1
LeftNavBarOutlineView selectedRowIndexes.count: 0
LeftNavBarOutlineView

The problem:

"sender" object is an NSTextField instance. I need to retrieve the "item" as returned by my func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any function.

I either need to get the "item" directly, or the row index (which I can retrieve the "item" from my map-dictionary)

Since the "sender" is an NSTextField, how can I find out what is the row (or "item") which has just been edited?

Thanks!


Solution

  • Get the row with row(for view and the item with item(atRow

    @IBAction func handleTextFieldDidEndEditing(_ sender: NSTextField) {
        let row = outlineView.row(for: sender)
        let item = outlineView.item(atRow: row)
    

    outlineView is the NSOutlineView instance