如何在IOS中使用两个谓词过滤UITableView

我有tableView与searchDisplayController.在这个电视中,我必须要数组(名/姓)
我可以用这个代码使用谓词过滤这个值
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.firstName beginswith[cd]%@",searchString];
self.filteredAllClients = [AllClients filteredArrayUsingPredicate:predicate];

我可以使用两个谓词过滤这个数组吗?

例如:我有名字(杰克·斯通,迈克·兰戈)

>如果我进入’J’,我应该得到过滤数组 – 杰克·斯通
>但是如果我进入’R’,我应该得到过滤的区域 – 麦克·兰戈?

解决方法

是的,像这样…
NSPredicate *firstNamePredicate = [NSPredicate predicateWithFormat:@"self.firstName beginswith[cd]%@",searchString];
NSPredicate *lastNamePredicate = [NSPredicate predicateWithFormat:@"self.lastName beginswith[cd]%@",searchString];

NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[firstNamePredicate,lastNamePredicate]];

self.filteredAllClients = [AllClients filteredArrayUsingPredicate:compoundPredciate];

然后,这将显示所有以名字从搜索开始的人,或者姓氏以搜索开始.

我更喜欢使用这种格式在单个谓词中使用OR和AND,因为它使整个代码更易读.

这也可以用来获得谓词的NOT.

如果您使用单个谓词内置复合谓词,也可以很容易地感到困惑.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...