过滤 NSOutlineView/NSTreeController
如何使用搜索框来过滤 NSOutlineView/NSTreeController? 我知道这与绑定和谓词有关。 但没有具体说怎么做。 有人可以带我完成过滤 NSOutlineView/NSTreeController 的步骤吗?
How would I use a search box to filter a NSOutlineView/NSTreeController? I know it would have something to do with bindings and a predicate. But not specificaly how to. Could someone take me through the steps of filtering an NSOutlineView/NSTreeController?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你不能。 NSArrayController 允许你给它一个过滤谓词; NSTreeController 没有。 我建议您提交错误。
I don't think you can. NSArrayController allows you to give it a filter predicate; NSTreeController doesn't. I suggest you file a bug.
从 macOS 10.11 开始,
NSTableView
(以及子类NSOutlineView
)具有新的hideRows
和unhideRows
方法简化了过滤行的任务。 仍然没有自动支持过滤NSTreeController
中的项目(它不是NSArrayController
的子类,因此不会继承其filter
谓词),但它至少做了很多繁重的工作,允许您将整个模型保留在控制器中,同时只显示它的一个子集。As of macOS 10.11,
NSTableView
(and therefore the subclassNSOutlineView
) has newhideRows
&unhideRows
methods that simplify the task to filtering out rows. There's still no automatic support for filtering out items inNSTreeController
(which is not a subclass ofNSArrayController
, and thus does not inherit itsfilter
predicate), but it at least does a lot of the heavy lifting of allowing you to keep the whole model in the controller while only displaying only a subset of it.我在这个问题上绞尽脑汁,但实时过滤
NSTreeController
实际上非常简单,您只需调整实际的节点对象即可。就我而言,我放弃了在控制器上实现实际过滤器,只是将谓词传递到我的树节点上。 当谓词在叶节点上不匹配时,我只需将其从子数组中删除即可。
现在,您可以将
NSTreeController
类名称设置为PredicateOutlineNode
并将其关键路径设置为filteredChildren
、count
和isLeaf
。 当然,您可以以不同的方式命名对象访问器。 现在,当我想要过滤树时,我在根节点上设置一个 NSPredicate ,将其向下传递。也适用于 KVO,不需要额外的代码,
NSOutlineView
将自动更新。I wrecked my brain for quite a bit on this one, but its actually remarkably simple to live filter an
NSTreeController
, you just have to adapt your actual node object.In my case I gave up on implementing the actual filter on the controller and just passed a predicate down my tree nodes. When the predicate doesn't match on a leaf node, I simply removes it from the children array.
Now you can set your
NSTreeController
class name toPredicateOutlineNode
and its key paths tofilteredChildren
,count
andisLeaf
. You can name your object accessors differently of course. Now when I want to filter the tree, I set anNSPredicate
on the root node, which passes it down.Also works with KVO, no additional code required, the
NSOutlineView
will update automatically.