监听文本框改变:

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. [BZOJ 1228] E&D

    Link:https://www.lydsy.com/JudgeOnline/problem.php?id=1228 Solution: 感觉自己对博弈论的理论一直了解得不够透彻 一篇讲原理的文章:S ...

  2. python3开发进阶-Django框架中的ORM的常用操作的补充(F查询和Q查询,事务)

    阅读目录 F查询和Q查询 事务 一.F查询和Q查询 1.F查询 查询前的准备 class Product(models.Model): name = models.CharField(max_leng ...

  3. Java高级架构师(一)第07节:远程使用以及冲突解决

  4. 如何在mac中通过命令行使用sublime

    ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" ~/bin/subl 后续就可以通过  ...

  5. Linux 命令缩写部分解释

    转:http://blog.chinaunix.net/uid-28408358-id-3890783.html bin = BINaries  /dev = DEVices  /etc = ETCe ...

  6. 【js】判断浏览器是否IE浏览器

    搜罗各种方法来判断浏览器是否为IE浏览器 1.最简单的[来自:http://www.cnblogs.com/heganlin/p/5889743.html] if(!+[1,]){ layer.msg ...

  7. JVM Object Query Language (OQL) 查询语言

    Object Query Language (OQL) OQL is SQL-like query language to query Java heap. OQL allows to filter/ ...

  8. 《linux 内核全然剖析》 chapter 4 80x86 保护模式极其编程

    80x86 保护模式极其编程       首先我不得不说.看这章真的非常纠结...看了半天.不知道这个东西能干嘛.我感觉唯一有点用的就是对于内存映射的理解...我假设不在底层给80x86写汇编的话.我 ...

  9. XPath注入技术综述

    一次完整的 XPath 注入攻击应该包括使用特殊构造的查询来提取一个 XML 数据库内的 数据或者信息.作为一门新的技术,XPath 注入在一定程度上和 SQL 注入漏洞有着惊人的相 似之处,通过下面 ...

  10. ubuntu查看系统版本

    1.查看文件信息,包含32-bit就是32位,包含64-bit就是64位 root@HDController:/home/nulige/tools# uname -a Linux HDControll ...