如何在编辑模式下将项目添加到 NSOutlineView 中?

How to add item into NSOutlineView in the edit mode?

我正在使用 NSOutlineView,一切正常,我可以在单击 table 单元格时进入编辑模式。

但是,我想在插入新行时自动进入编辑模式。

我尝试了 makeFirstResponder 方法,但它不起作用。

这是我所做的:

    // -------------------------------------------------------------------------------
    //  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
    }

提前致谢。

self.treeController.insert(node, atArrangedObjectIndexPath: indexPath) 之后,您可以使用

开始编辑
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)
            }
        }
    }
})