问题描述
我想将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)
viewModel
中的
items
包含一个对象,该对象又具有自己的子级数组.
items
in viewModel
contains an object which in turn has its own array of children.
但是,进行上述绑定时,UI中什么都没有显示.我也尝试添加一列:
However, when doing the above binding, nothing shows in the 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)
但无济于事,我仍然没有在UI中呈现任何结果.
But to no avail, I still don't get any results rendered in the UI.
要使绑定有效,我缺少什么? NSOutlineView呈现预期的效果,只是其中没有任何项.
What am I missing to make the binding work? The NSOutlineView renders as expected, just with no items in.
(在Objective-C中随时回答,我对语言选择不挑剔)
推荐答案
您需要设置树控制器的childrenKeyPath
.否则,树控制器将无法理解其指向的数据结构. childrenKeyPath
应该是相对于items
数组元素的关键路径,该路径给出该元素的子元素数组.
You need to set the childrenKeyPath
of the tree controller. 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.
这篇关于用代码将NSTreeController绑定到NSOutlineView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!