在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的使用的更多相关文章

  1. iOS UISearchController 的使用方法

    iOS UISearchController 的使用方法 UISearchController 让用户在 UISearchBar 上输入搜索关键词,展示搜索结果或者进行其他操作.UISearchCon ...

  2. iOS - UISearchController

    前言 NS_CLASS_DEPRECATED_IOS(3_0, 8_0, "UISearchDisplayController has been replaced with UISearch ...

  3. iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)

    在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...

  4. iOS UISearchBar UISearchController

    搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 交,O2O还是在线教育等都会有一个搜索栏的实现,不过彼此实现效果是 ...

  5. iOS中的两种搜索方式UISearchDisplayController和UISearchController

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 以前iOS的搜索一般都使用UISearchDisplayCon ...

  6. iOS中 UISearchController 搜索栏 UI技术分享

    <p style="margin-top: 0px; margin-bottom: 0px; font-size: 20px; font-family: 'STHeiti Light' ...

  7. iOS开发-搜索栏UISearchBar和UISearchController

    iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 ...

  8. [转] iOS开发-搜索栏UISearchBar和UISearchController

    原文网址: http://www.cnblogs.com/xiaofeixiang/p/4273620.html?utm_source=tuicool iOS中UISearchDisplayContr ...

  9. iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)

    在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...

随机推荐

  1. Javascript刷题 》 查找数组元素位置

    找出元素 item 在给定数组 arr 中的位置 输出描述: function indexOf(arr, item) { ..... } 如果数组中存在 item,则返回元素在数组中的位置,否则返回 ...

  2. C#设计模式系列:观察者模式(Observer)

    在软件构建过程中,需要为某些对象建立一种“通知依赖关系”,即一个对象的状态发生改变,所有的依赖对象都需要得到通知. 1.观察者模式简介 1.1>.定义 定义对象间的一种一对多的依赖关系,当一个对 ...

  3. SQL Server中CTE的另一种递归方式-从底层向上递归

        SQL Server中的公共表表达式(Common Table Expression,CTE)提供了一种便利的方式使得我们进行递归查询.所谓递归查询方便对某个表进行不断的递归从而更加容易的获得 ...

  4. Math的三个将小数值舍入为整数方法

    舍入规则: Math.ceil():执行向上舍入,即它总是将数值向上舍入为最接近的整数: Math.floor():执行向下舍入,即它总是将数值向下舍入为最接近的整数: Math.round():执行 ...

  5. 【结果很简单,过程很艰辛】记阿里云Ons消息队列服务.NET接口填坑过程

    Maybe 这个问题很简单,因为解决方法是非常简单,但填坑过程会把人逼疯,在阿里云ONS工作人员.同事和朋友的协助下,经过一天的调试和瞎捣鼓,终于解决了这个坑,把问题记下来,也许更多人在碰到类似问题的 ...

  6. [OpenCV] Samples 13: opencv_version

    cv::CommandLineParser的使用. I suppose CommandLineParser::has("something") should be true whe ...

  7. iOS 使用EZAudio库生成wav出错的情况

    使用EZAudio库 录M4A格式可以参考该库例子中的代码. 录wav格式得改下源码.看下面的代码 AVAudioSession *session = [AVAudioSession sharedIn ...

  8. java 中多线程的同步函数的运用

    /* * 需求: * 银行有一个金库 * 有两个储户,分别存300元.每次存100 , 存三次 * * 这个是有线程问题的, * * 我们应该通过下边的三个方法来查找问题 * 1.明确哪些代码是多线程 ...

  9. 实验记录:Oracle redo logfile的resize过程

    实验记录:Oracle redo logfile的resize过程. 实验环境:RHEL 6.4 + Oracle 11.2.0.3 单实例 文件系统 实验目的:本实验是修改redo logfile的 ...

  10. 重温JSP学习笔记--三大指令九大内置对象

    最近在温习javaweb的相关基础知识,鉴于我弄丢了记满了整整一本的笔记,决定以后把笔记和一些学习上的心得以及碰到的一些问题统统都放在网上,今天看了一下jsp的相关基础,以下是笔记: JSP三大指令: ...