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

时间:2018-02-08 08:28:54

标签: swift cocoa nstextfield nsoutlineview nstablecellview

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

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

我使用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
    }

提前致谢。

1 个答案:

答案 0 :(得分:0)

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