如何创建过滤主表视图的iTunes样式源列表?

时间:2011-04-02 12:10:05

标签: cocoa nstableview nspredicate

我已经阅读了一些关于如何模仿iTunes源列表样式的主题。但我似乎无法弄清楚需要做些什么才能让他们在主表视图中显示其他内容。

我的设置是这样的:我在后台有核心数据,包含iTunes中的所有曲目和一个有3种状态的“状态”字符串。我想只在选择源列表中的项目时显示部分标题。源列表项与轨道的状态匹配。 换句话说:3个源列表项表示一个数据集中的3个不同组。这些组与这个状态变量区分开来。

我试图创建一个NSStrings和NSPredicates数组,并将所选项绑定到主表视图过滤谓词。但它没有用。

非常感谢您的回复。

编辑:从数组设置过滤谓词现在可以正常工作。但这对过滤表的NSSearch字段不适用。还有其他方法或者我可以轻松地组合这两个谓词吗?

1 个答案:

答案 0 :(得分:1)

您可以通过“OR”或“AND”将两个谓词“组合”在一起。换句话说,假设您有这两个谓词:

NSPredicate *one = [NSPredicate predicateWithFormat:@"foo = 42"];
NSPredicate *two = [NSPredicate predicateWithFormat:@"bar = 'abc'"];

你可以这样做:

NSArray *subpredicates = [NSArray arraWithObjects:one, two, nil];
NSPredicate *both = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];
//this is equivalent to: foo = 42 AND bar = 'abc'

或者

NSPredicate *both = [NSCompoundPredicate orPredicateWithSubpredicates:subpredicates];
//this is equivalent to: foo = 42 OR bar = 'abc'