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

前端之家收集整理的这篇文章主要介绍了如何在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.

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

原文链接:https://www.f2er.com/iOS/329142.html

猜你在找的iOS相关文章