使用UISearchDisplayController
使用UISearchDisplayController

虽然UISearchDisplayController名字中带有controller,可他不是一个UIView相关的controller,因为,切换显示UISearchDisplayController的时候,它并没有在UIWindow中加载(UIWindow一次只能加载一个controller,所以可以证明它不属于controller).
可以看看,他是继承自NSObject,是多个控件组合在一起的一个东西.

以下是使用时的效果图:

源码:
//
// RootViewController.m
// TableViewSearch
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h" @interface RootViewController ()
<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate> @property (nonatomic, strong) UITableView *dataView; // tableView
@property (nonatomic, strong) UISearchDisplayController *searchController; // 搜索控制器 @property (nonatomic, strong) NSArray *names; // 数据源
@property (nonatomic, strong) NSArray *searchResults; // 搜索结果 @end #define APP_HEIGHT [UIScreen mainScreen].applicationFrame.size.height
#define SCR_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SCR_WIDTH [UIScreen mainScreen].bounds.size.width @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 初始化tableView
_dataView = [[UITableView alloc] initWithFrame:CGRectMake(, , SCR_WIDTH, SCR_HEIGHT - )
style:UITableViewStylePlain];
_dataView.delegate = self;
_dataView.dataSource = self;
[self.view addSubview:_dataView]; // 初始化searchBar
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
searchBar.delegate = self;
searchBar.placeholder = @"搜索";
[searchBar sizeToFit]; // 将searchBar放置在tableView的HeaderView中
self.dataView.tableHeaderView = searchBar; // 初始化search控制器并获取searchBar以及当前视图控制器
_searchController = \
[[UISearchDisplayController alloc] initWithSearchBar:searchBar
contentsController:self];
_searchController.delegate = self;
_searchController.searchResultsDataSource = self;
_searchController.searchResultsDelegate = self; // tableView的数据源
_names = @[@"Aaliyah", @"Aaron", @"Abigail", @"Adam", @"Addison", @"Adrian", @"Aiden", @"Alex", @"Alexa", @"Alexander", @"Alexandra", @"Alexis", @"Allison", @"Alyssa", @"Amelia", @"Andrea", @"Andrew", @"Angel", @"Anna", @"Annabelle", @"Anthony", @"Aria", @"Ariana", @"Arianna", @"Ashley", @"Aubree", @"Aubrey", @"Audrey", @"Austin", @"Autumn", @"Ava", @"Avery", @"Ayden", @"Bailey", @"Bella", @"Benjamin", @"Bentley", @"Blake", @"Brandon", @"Brayden", @"Brianna", @"Brody", @"Brooklyn", @"Bryson", @"Caleb", @"Cameron", @"Camila", @"Carlos", @"Caroline", @"Carson", @"Carter", @"Charles", @"Charlotte", @"Chase", @"Chloe", @"Christian", @"Christopher", @"Claire", @"Colton", @"Connor", @"Cooper", @"Damian", @"Daniel", @"David", @"Dominic", @"Dylan", @"Easton", @"Eli", @"Elijah", @"Elizabeth", @"Ella", @"Ellie", @"Emily", @"Emma", @"Ethan", @"Eva", @"Evan", @"Evelyn", @"Faith", @"Gabriel", @"Gabriella", @"Gavin", @"Genesis", @"Gianna", @"Grace", @"Grayson", @"Hailey", @"Hannah", @"Harper", @"Henry", @"Hudson", @"Hunter", @"Ian", @"Isaac", @"Isabella", @"Isaiah", @"Jace", @"Jack", @"Jackson", @"Jacob", @"James", @"Jasmine", @"Jason", @"Jaxon", @"Jayden", @"Jeremiah", @"Jocelyn", @"John", @"Jonathan", @"Jordan", @"Jose", @"Joseph", @"Joshua", @"Josiah", @"Juan", @"Julia", @"Julian", @"Justin", @"Katherine", @"Kayden", @"Kayla", @"Kaylee", @"Kennedy", @"Kevin", @"Khloe", @"Kimberly", @"Kylie", @"Landon", @"Lauren", @"Layla", @"Leah", @"Levi", @"Liam", @"Lillian", @"Lily", @"Logan", @"London", @"Lucas", @"Lucy", @"Luis", @"Luke", @"Lydia", @"Mackenzie", @"Madeline", @"Madelyn", @"Madison", @"Makayla", @"Mason", @"Matthew", @"Maya", @"Melanie", @"Mia", @"Michael", @"Molly", @"Morgan", @"Naomi", @"Natalie", @"Nathan", @"Nathaniel", @"Nevaeh", @"Nicholas", @"Noah", @"Nolan", @"Oliver", @"Olivia", @"Owen", @"Parker", @"Peyton", @"Piper", @"Reagan", @"Riley", @"Robert", @"Ryan", @"Ryder", @"Samantha", @"Samuel", @"Sarah", @"Savannah", @"Scarlett", @"Sebastian", @"Serenity", @"Skylar", @"Sofia", @"Sophia", @"Sophie", @"Stella", @"Sydney", @"Taylor", @"Thomas", @"Trinity", @"Tristan", @"Tyler", @"Victoria", @"Violet", @"William", @"Wyatt", @"Xavier", @"Zachary", @"Zoe", @"Zoey"];
} //===============================================
#pragma mark -
#pragma mark - tableView相关代理
//===============================================
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.dataView)
{
return [self.names count];
}
else
{
return [self.searchResults count];
}
} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath
animated:YES];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reusedStr = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedStr];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reusedStr];
} if (tableView == self.dataView)
{
cell.textLabel.text = self.names[indexPath.row];
}
else
{
cell.textLabel.text = self.searchResults[indexPath.row];
} return cell;
} //===============================================
#pragma mark -
#pragma mark UISearchDisplayController相关代理
//=============================================== - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
NSLog(@"将要开始搜索");
} - (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
NSLog(@"开始搜索");
} - (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
NSLog(@"将要结束搜索");
} - (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
NSLog(@"结束搜索");
} - (void)searchDisplayController:(UISearchDisplayController *)controller
didLoadSearchResultsTableView:(UITableView *)tableView
{
NSLog(@"加载了tableView");
} - (void)searchDisplayController:(UISearchDisplayController *)controller
willUnloadSearchResultsTableView:(UITableView *)tableView
{
NSLog(@"卸载了tableView");
} - (void)searchDisplayController:(UISearchDisplayController *)controller
willShowSearchResultsTableView:(UITableView *)tableView
{
NSLog(@"将要显示tableView");
} - (void)searchDisplayController:(UISearchDisplayController *)controller
didShowSearchResultsTableView:(UITableView *)tableView
{
NSLog(@"已经显示了tableView");
} - (void)searchDisplayController:(UISearchDisplayController *)controller
willHideSearchResultsTableView:(UITableView *)tableView
{
NSLog(@"将要隐藏tableView");
} - (void)searchDisplayController:(UISearchDisplayController *)controller
didHideSearchResultsTableView:(UITableView *)tableView
{
NSLog(@"已经隐藏了tableView");
} - (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
NSLog(@"要重新加载tableView显示搜索的数据么?"); NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", searchString];
self.searchResults = [self.names filteredArrayUsingPredicate:predicate]; return YES;
} - (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchScope:(NSInteger)searchOption
{
NSLog(@"要重新加载tableView的搜索域么?");
return YES;
} @end
UISearchDisplayController自带了tableView,也是走的代理方法,与你的controller中的tableView一样的代理方法,这点需要注意:


使用UISearchDisplayController的更多相关文章
- iOS--- UITableView  + UISearchDisplayController - - - - -实现搜索功能
		
iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 ...
 - iOS--UISearchBar和UISearchDisplayController
		
UISearchBar继承自UIView.UIResponder.NSObject 属性: autocapitalizationType————自动对输入文本对象进行大小写设置(包含4种类型,但是有时 ...
 - UISearchBar和 UISearchDisplayController的使用
		
感觉好多文章不是很全面,所以本文收集整合了网上的几篇文章,感觉有互相补充的效果. 如果想下载源码来看:http://code4app.com/search/searchbar .本源码与本文无关 1. ...
 - UISerachBar / UISearchDisplayController
		
1. UISerachBar 继承与UIView, 包含uitextfield, 并且实现了uitextfielddelegate代理的主要内容 含有取消按钮, 默认不显示 2. UISerachDi ...
 - 如何在UINavigationBar上添加UISearchBar以及UISearchDisplayController的使用 --OC --iOS
		
那我们开始吧,下面是Sely写的一个Demo,分享给大家. 新建一个项目, UISearchDisplayController 的 displaysSearchBarInNavigationBar太死 ...
 - UISearchDisplayController简单使用
		
最近在做一个简单的app入门,中间有一个页面用到了搜索框,本来以为很简单的控件,没想到用到的时候才发现很麻烦. 搜索框使用过程大约有以下几个状态:不活跃-活跃-输入关键词-根据关键词动态列出相关结果- ...
 - iOS 用UISearchDisplayController实现查找功能
		
UISearchDisplayController是iOS中用于处理搜索功能的控制器,此控制器需要和UISearchBar结合使用 示例代码如下: // // WKRootViewController ...
 - Swift - 带结果列表的搜索条(UISearchDisplayController)的用法
		
(注:自iOS8起,苹果便废弃UISearchDisplayController的使用,改为使用UISearchController来实现类似功能,可参考我的另一篇文章“Swift - 使用UISea ...
 - 点击搜索取消UISearchDisplayController的搜索状态
		
一般,我们用到UISearchDisplayController的时候,都是须要对一个数据源进行刷选,在UISearchDisplayController自带的tableView中展示出来,然后点击退 ...
 - iOS UISearchDisplayController学习笔记
		
UISearchDisplayController和UISearchBar一起使用用来管理UISearchBar和搜索结果的展示.UISearchDisplayController提供了显示搜索结果的 ...
 
随机推荐
- mysql RR下不存在则插入
			
主要看并发事务中不存在则插入(只有key索引)的阻塞情况. 表定义: mysql> desc user; +-------------+------------------+------+--- ...
 - NoSQL数据库--简介
			
一.What's NoSQL? NoSQL,全称是”Not Only Sql”,指的是非关系型的数据库.这类数据库主要有这些特点:非关系型的.分布式的.开源的.水平可扩展的.原始的目的是为了大规模we ...
 - 产环境部署node记录(三): centOS 7 mySQL和mongoDB的安装
			
[mySQL的安装]: CentOS7默认数据库是mariadb,现在来安装mySQL 1.下载安装包 这里下载了四个安装包,后面会用到 yum -y install perl perl-devel ...
 - 开源方案搭建可离线的精美矢量切片地图服务-4.Mapbox样式设计
			
项目成果展示(所有项目文件都在阿里云的共享云虚拟主机上,访问地图可以会有点慢,请多多包涵). 01:中国地图:http://test.sharegis.cn/mapbox/html/3china.ht ...
 - Golang cron 定时任务使用
			
1.cron 表达式的基本格式 用过 linux 的应该对 cron 有所了解.linux 中可以通过 crontab -e 来配置定时任务.不过,linux 中的 cron 只能精确到分钟.而我们这 ...
 - [转]ng-grid Auto / Dynamic Height
			
本文转自:https://stackoverflow.com/questions/23396398/ng-grid-auto-dynamic-height I think I solved this ...
 - Hadoop shell 一查就会
			
Hadoop shell 命令有三种格式 hdfs + dfs (必须是dfs) Hadoop + dfs Hadoop + df 命令 说明 hadoop 版本查看 hadoop version h ...
 - Linux下socket通信和多线程
			
服务端socket流程:socket() –> bind() –> listen() –> accept() –> 读取.发送信息(recv,send等) 客户端socket流 ...
 - Linux 文件缓存 (一)
			
缓存印象 缓存给人的感觉就是可以提高程序运行速度,比如在桌面环境中,第一次打开一个大型程序可能需要10秒,但是关闭程序后再次打开可能只需5秒了.这是因为运行程序需要的代码.数据文件在操作系统中得到了缓 ...
 - grafana 安装- 曲线图展示每秒新增数据量
			
下载: https://dl.grafana.com/oss/release/grafana-5.4.2.windows-amd64.zip 解压就能用 添加数据源 添加查询条件 sql 模式编写查询 ...