NSOutlineView 绑定到 NSTreeController(节点在 window 调整大小时折叠)
NSOutlineView bound to NSTreeController (nodes collapse on window resize)
我可以使用 Interface Builder 成功地将 NSTreeController 绑定到 NSOutlineView。当我尝试以编程方式做同样的事情时,我也可以让它工作,除了一个小问题。
调整大纲视图固定到的 window 的大小会折叠所有打开的项目。如果我以其他方式调整大纲视图的大小,例如将其放入 NSSplitView,上下移动滑块不会导致任何问题。只有当我调整 window.
大小时才会发生
大纲视图只是 Interface Builder 中的默认视图。我把它放在 window 上,固定了所有四个边,将插座连接到我的 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
}
viewWillLayout() 是设置树控制器和大纲视图的错误位置。它被反复调用,不适用于此类事情。
将所有内容移至 viewDidLoad() 并解决了问题。
我可以使用 Interface Builder 成功地将 NSTreeController 绑定到 NSOutlineView。当我尝试以编程方式做同样的事情时,我也可以让它工作,除了一个小问题。
调整大纲视图固定到的 window 的大小会折叠所有打开的项目。如果我以其他方式调整大纲视图的大小,例如将其放入 NSSplitView,上下移动滑块不会导致任何问题。只有当我调整 window.
大小时才会发生大纲视图只是 Interface Builder 中的默认视图。我把它放在 window 上,固定了所有四个边,将插座连接到我的 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
}
viewWillLayout() 是设置树控制器和大纲视图的错误位置。它被反复调用,不适用于此类事情。
将所有内容移至 viewDidLoad() 并解决了问题。