我有带有 searchDisplayController
的 tableView。在这台电视中,我必须排列(名字/姓氏)
我可以使用谓词和这段代码按名称过滤这些值
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.firstName beginswith[cd]%@",searchString];
self.filteredAllClients = [AllClients filteredArrayUsingPredicate:predicate];
我可以使用两个谓词过滤这个数组吗?
例如:我有名字(Jack Stone、Mike Rango)
如果我输入“J”并且我应该得到过滤数组 - Jack Stone
但如果我输入“R”,我应该得到过滤区域 - Mike Rango?
最佳答案
是的,像这样......
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。
如果您在单个谓词中构建复合谓词,您也很容易感到困惑。
关于ios - 如何在 IOS 中使用两个谓词过滤 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15498795/