简体   繁体   English

使用NSTreeController过滤NSOutlineView的子级

[英]Filtering children of NSOutlineView with NSTreeController

I am developing a Mac app which uses Core data. 我正在开发使用Core数据的Mac应用程序。 I am using NSOutlineView and NSTreeController to bind data on view. 我正在使用NSOutlineView和NSTreeController绑定视图上的数据。

You can assume the structure of my data as 您可以假设我的数据结构为

  • Parent Item 1 父项1
    • Child Item 1 儿童项目1
    • Child Item 2 子项2
    • Child Item 3 儿童项目3
  • Parent Item 2 父项2
    • Child Item 4 儿童4
    • Child Item 5 儿童5

I am applying a fetchPredicate to my NSTreeController to filter the data successfully. 我将fetchPredicate应用于我的NSTreeController以成功过滤数据。 On the other hand, the fetchPredicate is applied only to the first level of the data (to parent Items). 另一方面,fetchPredicate仅应用于数据的第一级(应用于父项)。

I need a method which I can apply a fetchPredicate also to the child items. 我需要一种可以将fetchPredicate也应用于子项的方法。 For example if my criteria matches to Child Item 1 and Child Item 4 the result should be 例如,如果我的标准与子项1和子项4相匹配,则结果应为

  • Parent Item 1 父项1
    • Child Item 1 儿童项目1
  • Parent Item 2 父项2
    • Child Item 4 儿童4

Any help will be appreciated. 任何帮助将不胜感激。

You're applying a fetch predicate. 您正在应用提取谓词。 NSTreeController doesn't support a filter predicate. NSTreeController不支持过滤谓词。 NSTreeController uses the fetch predicate to fetch the top level objects and uses the children relationship to get the children. NSTreeController使用fetch谓词来获取顶级对象,并使用children关系来获取child。 The children aren't fetched and the fetch predicate is not used to get the children. 不获取子项,并且不使用获取谓词来获取子项。

Solution 1: implement a calculated property filteredChildren , like in this question: Filtering A Tree Controller and mentioned in this unrelated answer: Is it possible to bind an NSTreeController to an NSOutlineViewDataSource? 解决方案1:实现一个计算所得的属性filteredChildren ,例如以下问题: 过滤树控制器,并在以下不相关的答案中提到: 是否可以将NSTreeController绑定到NSOutlineViewDataSource?

Solution 2: use a data source instead of bindings, also mentioned in the above answer. 解决方案2:使用数据源代替绑定,上面的答案中也提到了绑定。

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

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