监听文本框改变:

DJSelectCityViewController.m

/** 当searchBar内的文字发生改变时调用此方法 */
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { UIView *cover = [self.view viewWithTag:DJCoverTag];
if (searchText.length) { // 当前输入内容不为空
if(cover.subviews.count <= ) {
cover.alpha = 1.0;
self.searchResultVC.view.frame = CGRectMake(, , cover.width, cover.height);
[cover addSubview:self.searchResultVC.view];
}
// 将当前内容传递给 DJSearchCityResultViewController 以进行搜索
[self.searchResultVC setSearchText:searchText];
} else { // 当前输入内容为空
[self.searchResultVC.view removeFromSuperview];
cover.alpha = 0.2;
} }

DJSearchCityResultViewController.m

#import "DJSearchCityResultViewController.h"
#import "MJExtension.h"
#import "DJCity.h" @interface DJSearchCityResultViewController () /** 城市列表 */
@property (nonatomic,strong) NSArray *citiesList;
/** 搜索匹配到的结果 */
@property (nonatomic,strong) NSArray *matchSearchResults; @end @implementation DJSearchCityResultViewController /** 加载城市列表 */
- (NSArray *)citiesList { if (!_citiesList) {
_citiesList = [DJCity mj_objectArrayWithFilename:@"cities.plist"];
}
return _citiesList;
} - (void)viewDidLoad {
[super viewDidLoad]; } /** 设置搜索内容 */
- (void)setSearchText:(NSString *)searchText { // 将待搜索字符串转换成小写
NSString *searchFormat = searchText.lowercaseString;
// 使用谓词进行搜索
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains %@ or pinYin contains %@ or pinYinHead contains %@",searchFormat,searchFormat,searchFormat];
self.matchSearchResults = [self.citiesList filteredArrayUsingPredicate:predicate];
// 匹配完成后刷新tableView
[self.tableView reloadData]; } #pragma mark - UITableView数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.matchSearchResults.count; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [NSString stringWithFormat:@"搜索到%ld条结果",self.matchSearchResults.count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"matchResult";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
DJCity *city = self.matchSearchResults[indexPath.row];
cell.textLabel.text = city.name;
return cell;
} @end

最终结果:

美团HD(8)-利用NSPredicate匹配搜索结果的更多相关文章

  1. poj 2446 Chessboard (二分图利用奇偶性匹配)

    Chessboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13176   Accepted: 4118 Descr ...

  2. Jqgrid利用正则匹配表达式正确移除html标签

    在使用JqGrid表格插件过程中,遇到一个问题:后台取出来的字段是带有Html标签的,于是将内容填充到表格之后,带有的html标签会把表格撑开或者每一行的内容显示不统一,导致非常难看,就像下图所示: ...

  3. Excel-单条件和多条件匹配搜索

    6.[单条件匹配搜索]有两个表格(姓名列,年龄列,收入列等),从表1总表中,把表2中人员的年龄和收入匹配出来: 方法一: 公式=VLOOKUP($S2,$O$2:$Q$5,2,0) #其中最后0< ...

  4. 美团HD(7)-添加取消搜索按钮

    DJSelectCityViewController.m #pragma mark - UISearchBar 代理方法 /** SearchBar开始编辑 */ - (void)searchBarT ...

  5. 美团HD(6)-添加搜索遮罩

    DJSelectCityViewController.m /** SearchBar开始编辑 */ - (void)searchBarTextDidBeginEditing:(UISearchBar ...

  6. grep精确匹配搜索某个单词的用法 (附: grep高效用法小结))

    grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正 ...

  7. 利用css实现搜索过滤

    无意中找到一种利用css就可实现的搜索过滤的方法,不得不说看了代码之后确实被惊艳到了,亏我之前面试还因为做这个功能做太慢而拖了后腿.在此记录下代码: <!DOCTYPE html> < ...

  8. Solr的精确匹配搜索

    情景: 利用Solr做一批词的逆文档频率.Solr中存储的每条数据为一篇文章,此时需要查出某词在多少篇文章中出现过,然后用公式:某词逆文档频率 = 总文章数 / (出现过某词的文章数+1) 来计算. ...

  9. solr多词匹配搜索问题及解决

    使用solr进行某较长词搜索时出现了一些问题,及解决方案. 1.问题:solr默认使用OR方式搜索,当搜索一个很长的次,比如“XX集团股份有限公司”,分词器分词后,使用OR方式匹配,会匹配到很多结果. ...

随机推荐

  1. [BZOJ4538]网络

    今天打比赛,毒瘤yww把这题出到$n,m\leq 5\times10^5$,因为不会写整体二分所以来写坑爹的$O\left(n\log_2n\right)$做法 考虑按重要度建权值线段树(相同权值的请 ...

  2. 【线性筛】【筛法求素数】【素数判定】URAL - 2102 - Michael and Cryptography

    暴力搞肯定不行,因此我们从小到大枚举素数,用n去试除,每次除尽,如果已经超过20,肯定是no.如果当前枚举到的素数的(20-已经找到的质因子个数)次方>剩下的n,肯定也是no.再加一个关键的优化 ...

  3. 正版greenvpn

    短网址 http://jsq.re(建议收藏,长期有效)长网址 https://www.greenjsq.me/网址更新页面 http://www.greenvpn.site

  4. High Speed Inter-CHIP USB 2.0 PHY

    转载:http://arasan.com/products/usb/usb-2-0/hsic-phy/ High Speed Inter-CHIP USB 2.0 PHY USB is the ubi ...

  5. HashMap深度解析(一)

    HashMap可以说是Java中最常用的集合类框架之一,是Java语言中非常典型的数据结构,我们总会在不经意间用到它,很大程度上方便了我们日常 开发.在很多Java的笔试题中也会被问到,最常见的,“H ...

  6. js 的 slice方法

    slice() 方法可从已有的数组中返回选定的元素. string.slice( beginslice [, endSlice] ); 下面是参数的详细信息: beginSlice : 从零开始的索引 ...

  7. 定期访问WebLogic Server返回状态的脚本

    在运维过程中,经常要获悉WebLogic Server的状态以便于主动的维护,本文通过weblogic WLST脚本初步设计了一下 脚本大概为2个,一是WLST的py脚本,getStates.py c ...

  8. dependency:copy-dependencies使用,如何排除应用自身module

    相信用法参考:https://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html#includeG ...

  9. php之防注入程序绕过浅谈

    <?php/*判断传递的变量是否含有非法字符如:$_POST/$_GET功能:SQL防注入系统*/ //屏蔽错误提示error_reporting(7); //需要过滤的字符 $ArrFiltr ...

  10. docker_usb开发软件部署

    1.docker镜像包  (备注:61提供,带桌面版本) rayosx2.0.2.tar 2.paho-mqtt dnf install git -y git clone https://github ...