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 ...
随机推荐
- SQL Server中的窗口函数
简介 SQL Server 2012之后对窗口函数进行了极大的加强,但对于很多开发人员来说,对窗口函数却不甚了解,导致了这样强大的功能被浪费,因此本篇文章主要谈一谈SQL Server中窗口函 ...
- 【Win10 应用开发】扫描和连接Wi-fi网络
老周今天带大家去“扫雷”了,别当真,是扫描并连接指定无线网络,时尚一点叫Wi-fi. 所以,今天的任务要求你的设备至少有1张无线网卡,目前老周没看到过有N张无线网卡的设备.像笔记本.平板等设备都可以, ...
- Eclipse开发工具组合键介绍
1. 编辑类 Ctrl + Q 跳到最后一次的编辑处 Ctrl + 1 快速修复提示 Alt + ↓ ...
- android帧动画,移动位置,缩放,改变透明度等动画讲解
1.苦逼的需求又来了,需要实现一些动画效果,第一个想到的是播放gif图片,但是这样会占包的资源,并且清晰度不高,于是想着程序实现,自己用帧动画+缩放+移动+透明度 实现了一些想要的效果,这里跟大家分享 ...
- Java 8新特性-4 方法引用
对于引用来说我们一般都是用在对象,而对象引用的特点是:不同的引用对象可以操作同一块内容! Java 8的方法引用定义了四种格式: 引用静态方法 ClassName :: staticMetho ...
- wireshark常用的过滤器设置
过滤源ip.目的ip.在wireshark的过滤规则框Filter中输入过滤条件.如查找目的地址为192.168.101.8的包,ip.dst==192.168.101.8:查找源地址为ip.src ...
- MySQL半同步复制
从MySQL5.5开始,MySQL以插件的形式支持半同步复制.如何理解半同步呢?首先我们来看看异步,全同步的概念 异步复制(Asynchronous replication) MySQL默认的复制即是 ...
- 让自己也能使用Canvas
<canvas> 是 HTML5 新增的元素,可使用JavaScript脚本来绘制图形.例如:画图,合成照片,创建动画甚至实时视频处理与渲染. 兼容性方面,除了一些骨灰级浏览器IE6.IE ...
- Cesium应用篇:3控件(4)Geocoder
Geocoder是一个非常简单的控件,但也是非常常用且实用的控件,顾名思义,Geocoder就是地理编码的意思,而平常我们经常会查询一些地物,也就是常用的POI搜索,就是Geocoder的功劳. 首先 ...
- HTML5+CSS3实现图片可倾斜摆放的动画相册效果
先看看效果:其中鼠标悬浮在图片上会有动态效果图 直接上代码: css文件 @CHARSET "UTF-8"; *{ padding:0px; margin:0px; } div{ ...