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

标签 ios objective-c uisearchdisplaycontroller

我有带有 searchDisplayController 的 tableView。在这台电视中,我必须排列(名字/姓氏) 我可以使用谓词和这段代码按名称过滤这些值

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.firstName beginswith[cd]%@",searchString];
self.filteredAllClients = [AllClients filteredArrayUsingPredicate:predicate];

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

例如:我有名字(Jack Stone、Mike Rango)

  1. 如果我输入“J”并且我应该得到过滤数组 - Jack Stone

  2. 但如果我输入“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/

相关文章:

iphone - 刷新 View

iOS 9 应用程序卡住,控制台日志 "BKSendHIDEvent"

ios - 在 UIBlurEffect 上画洞

ios - 如何在关闭 UISearchDisplayController 时将文本保留在搜索栏中

iphone - UISearchBar 在退出搜索模式时闪烁

ios - 在界面生成器中更改 SearchDisplayController 委托(delegate)

ios - Swift:在没有 TabBar 和 NavigationBar 的情况下裁剪屏幕截图

ios - WatchKit - 如何移动标签或图像

ios - quartz 动画错误

iphone - 动画移动 UIButton 在移动时不响应触摸/点击