NSOutlineView绑定到NSTreeController(节点在窗口调整大小时崩溃)

时间:2016-10-04 13:36:52

标签: swift nsoutlineview nstreecontroller

我可以使用Interface Builder将NSTreeController成功绑定到NSOutlineView。当我尝试以编程方式执行相同的操作时,除了一个小问题之外,我还可以让它工作。

调整大纲视图固定的窗口大小,折叠所有打开的项目。如果我以其他方式调整大纲视图的大小,例如将其置于NSSplitView中,则上下移动滑块不会导致任何问题。它只在我调整窗口大小时才会发生。

大纲视图只是Interface Builder中的默认视图。我将它放在一个窗口上,固定四面,将插座连接到我的ViewController,就是这样。

任何人都有洞察力?感谢。

以下是我以编程方式创建内容的方式:

override func viewWillLayout() {
    super.viewWillLayout()

    var treeController: NSTreeController!
    @IBOutlet weak var outlineView: NSOutlineView!
    var content = [TreeNode]()

    newTreeController = NSTreeController.init(content: nil)
    newTreeController.objectClass = TreeNode.self
    newTreeController.childrenKeyPath = "children"
    newTreeController.countKeyPath = "count"
    newTreeController.leafKeyPath = "leaf"
    newTreeController.preservesSelection = true
    newTreeController.isEditable = true
    newTreeController.bind("contentArray", to: self, withKeyPath: "content", options: nil)

    outlineView.delegate = self
    outlineView.bind("content", to: newTreeController, withKeyPath: "arrangedObjects", options: nil)
    outlineView.bind("selectionIndexPaths", to: newTreeController, withKeyPath: "selectionIndexPaths", options: nil)
}

override func viewDidLoad() {
    super.viewDidLoad()

    willChangeValue(forKey: "content")
    // POPULATE CONTENT ARRAY
    didChangeValue(forKey: "content")
}

// NSOutlineViewExtension

func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
    var cellView: NSTableCellView?
    if let identifier = tableColumn?.identifier {
        if let view = outlineView.make(withIdentifier: identifier, owner: outlineView.delegate) as? NSTableCellView {
            // view.imageView?.bind(NSValueBinding, to: view, withKeyPath: "objectValue.WHATEVER_PROPERTY", options: nil)
            // view.textField?.bind(NSValueBinding, to: view, withKeyPath: "objectValue.WHATEVER_PROPERTY", options: nil)
            cellView = view
        }
    }
    return cellView
}

1 个答案:

答案 0 :(得分:0)

viewWillLayout()是设置树控制器和大纲视图的错误位置。它被反复调用,并不适用于这类事情。

将所有内容移至viewDidLoad()并解决问题。