繁体   English   中英

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

[英]How to add item into NSOutlineView in the edit mode?

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

提前致谢。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM