简体   繁体   English

用代码将NSTreeController绑定到NSOutlineView

[英]Binding an NSTreeController to an NSOutlineView in code

I'm looking to bind an NSTreeController , which in turn is bound to an array, to an NSOutlineView : 我正在寻找将NSTreeController绑定到NSOutlineView ,后者又绑定到数组:

treeController.bind(NSContentArrayBinding,
    toObject: viewModel,
    withKeyPath: "items",
    options: nil)

outlineView.bind(NSContentBinding,
    toObject: treeController,
    withKeyPath: "arrangedObjects",
    options: nil)

outlineView.bind(NSSortDescriptorsBinding,
    toObject: treeController,
    withKeyPath: "sortDescriptors",
    options: nil)

outlineView.bind(NSSortDescriptorsBinding,
    toObject: treeController,
    withKeyPath: "selectionIndexPaths",
    options: nil)

items in viewModel contains an object which in turn has its own array of children. viewModel中的items包含一个对象,该对象又具有自己的子viewModel组。

However, when doing the above binding, nothing shows in the UI. 但是,进行上述绑定时,UI中什么也没有显示。 I've also tried adding a column: 我也尝试添加一列:

let column = NSTableColumn(identifier: "0")

column.bind(NSValueBinding,
    toObject: treeController,
    withKeyPath: "arrangedObjects.displayName"),
    options: nil)

outlineView.addTableColumn(column)

But to no avail, I still don't get any results rendered in the UI. 但无济于事,我仍然没有在UI中呈现任何结果。

What am I missing to make the binding work? 使绑定工作时我缺少什么? The NSOutlineView renders as expected, just with no items in. NSOutlineView呈现预期的效果,只是其中没有任何项。

(Feel free to answer in Objective-C, I'm not picky on language choice) (在Objective-C中随意回答,我对语言选择不挑剔)

You need to set the childrenKeyPath of the tree controller. 您需要设置树控制器的childrenKeyPath Otherwise, the tree controller can't understand the data structure that it's pointed to. 否则,树控制器将无法理解其指向的数据结构。 The childrenKeyPath should be the key path, relative to an element of the items array, that gives an array of that element's children. childrenKeyPath应该是相对于items数组元素的键路径,该路径给出了该元素的孩子的数组。

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

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