iOS UISearchController的使用
在iOS9中,UISearchDisplayController 已经被UISearchController替代。搜索框是一种常用的控件。
假设我们要满足下图的需求,产生100个“数字+三个随机字母”,然后搜索包含某个字母的结果。
那么,该怎么做呢?

#import "ViewController.h" @interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchControllerDelegate,UISearchResultsUpdating> //tableView
@property (strong, nonatomic) UITableView *tableView; //searchController
@property (strong, nonatomic) UISearchController *searchController; //数据源
@property (strong,nonatomic) NSMutableArray *dataList; @property (strong,nonatomic) NSMutableArray *searchList; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; _dataList = [NSMutableArray array];
_searchList = [NSMutableArray array]; self.dataList=[NSMutableArray arrayWithCapacity:]; //产生100个“数字+三个随机字母”
for (NSInteger i=; i<; i++) {
[self.dataList addObject:[NSString stringWithFormat:@"%ld%@",(long)i,[self shuffledAlphabet]]];
} _tableView = [[UITableView alloc]initWithFrame:CGRectMake(, ,[UIScreen mainScreen].bounds.size.width ,[UIScreen mainScreen].bounds.size.height)]; _tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSelectionStyleNone; //创建UISearchController
_searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
//设置代理
_searchController.delegate = self;
_searchController.searchResultsUpdater= self; //设置UISearchController的显示属性,以下3个属性默认为YES
//搜索时,背景变暗色
_searchController.dimsBackgroundDuringPresentation = NO;
//搜索时,背景变模糊
_searchController.obscuresBackgroundDuringPresentation = NO;
//隐藏导航栏
_searchController.hidesNavigationBarDuringPresentation = NO; _searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0); // 添加 searchbar 到 headerview
self.tableView.tableHeaderView = _searchController.searchBar; [self.view addSubview:_tableView]; // Do any additional setup after loading the view, typically from a nib.
} //产生3个随机字母
- (NSString *)shuffledAlphabet { NSMutableArray * shuffledAlphabet = [NSMutableArray arrayWithArray:@[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"]]; NSString *strTest = [[NSString alloc]init];
for (int i=; i<; i++) {
int x = arc4random() % ;
strTest = [NSString stringWithFormat:@"%@%@",strTest,shuffledAlphabet[x]];
} return strTest; } //设置区域的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (self.searchController.active) {
return [self.searchList count];
}else{
return [self.dataList count];
}
} //返回单元格内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *flag=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag];
}
if (self.searchController.active) {
[cell.textLabel setText:self.searchList[indexPath.row]];
}
else{
[cell.textLabel setText:self.dataList[indexPath.row]];
}
return cell;
} #pragma mark - UISearchControllerDelegate代理 //测试UISearchController的执行过程 - (void)willPresentSearchController:(UISearchController *)searchController
{
NSLog(@"willPresentSearchController");
} - (void)didPresentSearchController:(UISearchController *)searchController
{
NSLog(@"didPresentSearchController");
} - (void)willDismissSearchController:(UISearchController *)searchController
{
NSLog(@"willDismissSearchController");
} - (void)didDismissSearchController:(UISearchController *)searchController
{
NSLog(@"didDismissSearchController");
} - (void)presentSearchController:(UISearchController *)searchController
{
NSLog(@"presentSearchController");
} -(void)updateSearchResultsForSearchController:(UISearchController *)searchController { NSLog(@"updateSearchResultsForSearchController");
NSString *searchString = [self.searchController.searchBar text];
NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
if (self.searchList!= nil) {
[self.searchList removeAllObjects];
}
//过滤数据
self.searchList= [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:preicate]];
//刷新表格
[self.tableView reloadData];
}
UISearchController代理方法的执行过程:

UISearchController的使用步骤:
1、创建
//创建UISearchController
_searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
2、设置代理
//设置代理
_searchController.delegate = self;
_searchController.searchResultsUpdater= self;
3、设置属性
//设置UISearchController的显示属性,以下3个属性默认为YES
//搜索时,背景变暗色
_searchController.dimsBackgroundDuringPresentation = NO;
//搜索时,背景变模糊
_searchController.obscuresBackgroundDuringPresentation = NO;
//隐藏导航栏
_searchController.hidesNavigationBarDuringPresentation = NO;
4、实现代理
- (void)willPresentSearchController:(UISearchController *)searchController;
- (void)didPresentSearchController:(UISearchController *)searchController;
- (void)willDismissSearchController:(UISearchController *)searchController;
- (void)didDismissSearchController:(UISearchController *)searchController;
- (void)presentSearchController:(UISearchController *)searchController; - (void)updateSearchResultsForSearchController:(UISearchController *)searchController;
注意点:
1、如果你希望在同一个视图中显示搜索结果,则通过[[UISearchController alloc]initWithSearchResultsController:nil]。但是这是不支持TVOS,请提供TVOS一定要指定结果控制器。
[[UISearchController alloc]initWithSearchResultsController:VC],可以实现指定结果控制器。
iOS UISearchController的使用的更多相关文章
- iOS UISearchController 的使用方法
iOS UISearchController 的使用方法 UISearchController 让用户在 UISearchBar 上输入搜索关键词,展示搜索结果或者进行其他操作.UISearchCon ...
- iOS - UISearchController
前言 NS_CLASS_DEPRECATED_IOS(3_0, 8_0, "UISearchDisplayController has been replaced with UISearch ...
- iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...
- iOS UISearchBar UISearchController
搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 交,O2O还是在线教育等都会有一个搜索栏的实现,不过彼此实现效果是 ...
- iOS中的两种搜索方式UISearchDisplayController和UISearchController
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 以前iOS的搜索一般都使用UISearchDisplayCon ...
- iOS中 UISearchController 搜索栏 UI技术分享
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; font-family: 'STHeiti Light' ...
- iOS开发-搜索栏UISearchBar和UISearchController
iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 ...
- [转] iOS开发-搜索栏UISearchBar和UISearchController
原文网址: http://www.cnblogs.com/xiaofeixiang/p/4273620.html?utm_source=tuicool iOS中UISearchDisplayContr ...
- iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...
随机推荐
- jquery插件编写模版
jquery插件是什么??这里以讨论实力方法为主,比如 $("div").pluginname({}); 他的最简单形势应该是 $.prototype.plugin = funct ...
- 解密jQuery事件核心 - 模拟事件(四)
前几章已经把最核心的实现都分解过了,这一章我们看看jQuery是如何实现事件模拟的 在Internet Explorer 8和更低,一些事件change 和 submit本身不冒泡,但jQuery修改 ...
- MyEclipse 2014(激活)
激活 破解附件包下载:http://pan.baidu.com/s/1c27Dwe0 提取密码:qy38 详细破解步骤请参考:http://blog.my-eclipse.cn/myeclipse-2 ...
- C++基础知识
基础知识 &&和||具有"短路"特性,特别是在第二个操作数有++或--时要注意. 显式类型转换 (类型说明符)表达式 //C风格的 类型说明符(表达式) //cpp ...
- spring笔记--通过注解(annotation)配置Bean
Spring能够在classpath下自动扫描,侦测和实例化具有特定注解的组件,这在Spring中成为组件扫描(Component scanning). 特定组件的注解包括: @Component:基 ...
- 浅谈MITM攻击之信息窃取(解密315晚会报道的免费WIFI窃取个人信息)
前言 所谓的MITM攻击(即中间人攻击),简而言之就是第三者通过拦截正常的网络通信数据,并进行数据篡改和嗅探,而通信的双方毫无感知.这个很早就成为黑客常用的手段,一会聊的315晚会窃取个人信息只是 ...
- OpenGL新手框架
开始学习用OpenGL,也就想显示一些点,以为挺简单的,哎,看了两天才会画三维的点,做个总结. 使用OpenGL的基本流程 int main(int argv, char *argc[]) { //初 ...
- mybatis入门基础(七)----延迟加载
一.什么是延迟加载 resultMap可以实现高级映射(使用association.collection实现一对一及一对多映射),association.collection具备延迟加载功能. 需求: ...
- MathType应用:批量改变公式格式
首先要安装好mathtype,一般装好后mathtype会嵌入到word里去(黄色和粉红的部分是今天的主角) 然后可以用带insert开头的选项(黄色部分)添加公式,但是添加公式后可能出现一下情况,即 ...
- 为Linux重新开发MVC,有图有真相
1.写在前面 就连我们自己开始时也在问自己:我们为什么要开发一套MVC,微软的难道不可用用吗? 一开始的理由很简单.为了更好地跨平台部署;在Linux部署过.NET的人们应该知道, 部署起来是有点繁琐 ...