Search code examples
swiftcocoanstextfieldnsoutlineviewnstablecellview

How to add item into NSOutlineView in the edit mode?


I am working with NSOutlineView, and everything works well, I can enter edit mode when clicking on the table cell.

However, I want to enter the edit mode automatically when a new row is inserted.

I played around with makeFirstResponder method, but it doesn't work.

Here is what I did:

    // -------------------------------------------------------------------------------
    //  viewForTableColumn:tableColumn:item
    // -------------------------------------------------------------------------------
    func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
        var result = outlineView.make(withIdentifier: tableColumn?.identifier ?? "", owner: self)

        if let node = (item as! NSTreeNode).representedObject as? BaseNode {
            if self.outlineView(outlineView, isGroupItem: item) {    // is it a special group (not a folder)?
                // Group items are sections of our outline that can be hidden/shown (i.e. PLACES/BOOKMARKS).
                let identifier = outlineView.tableColumns[0].identifier

                result = outlineView.make(withIdentifier: identifier, owner: self) as! NSTableCellView?
                let value = node.nodeTitle
                (result as! NSTableCellView).textField!.stringValue = value
            } else if node.isSeparator {
                result = outlineView.make(withIdentifier: "SeparatorView", owner: self)
            } else {
                (result as! NSTableCellView).textField!.stringValue = node.nodeTitle
                (result as! NSTableCellView).imageView!.image = node.nodeIcon
                if node.isLeaf {
                     let textField = (result as! NSTableCellView).textField!
                     (result as! NSTableCellView).window?.makeFirstResponder(textField)
                    textField.isEditable = true // make leaf title's editable.
                    //### Translator's extra
                    textField.target = self
                    textField.action = #selector(self.didEditTextField(_:))
                }
            }
        }

        return result
    }

Thanks in advance.


Solution

  • After self.treeController.insert(node, atArrangedObjectIndexPath: indexPath) you can start edit with

    DispatchQueue.main.async(execute: {
        let node = self.treeController.arrangedObjects.descendant(at: indexPath)
        let row = self.outlineView.row(forItem: node)
        let view = self.outlineView.view(atColumn: 0, row: row, makeIfNecessary: false)
        if let cellView = view as? NSTableCellView {
            if let textField = cellView.textField {
                if textField.acceptsFirstResponder {
                    self.window.makeFirstResponder(textField)
                }
            }
        }
    })