使用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提供了显示搜索结果的 ...
随机推荐
- 代码查看php是否已开启rewrite功能模块
通过php代码来查询,是否把rewrite模块打开了 <?php $result = apache_get_modules(); if(in_array('mod_rewrite', $resu ...
- JDK的spi实现
SPI的全名为Service Provider Interface.大多数开发人员可能不熟悉,因为这个是针对厂商或者插件的.在java.util.ServiceLoader的文档里有比较详细的介绍.简 ...
- 剑指offer63:数据流中的中位数
题目描述: 如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值.如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值. ...
- python while嵌套循环
while循环1.输出打印以#组成的长方形,自己定义长和宽.# -*-encoding:utf-8-*-'''This is script for start docker containor!Aut ...
- Deep Residual Learning for Image Recognition(残差网络)
深度在神经网络中有及其重要的作用,但越深的网络越难训练. 随着深度的增加,从训练一开始,梯度消失或梯度爆炸就会阻止收敛,normalized initialization和intermediate n ...
- 前端项目中使用git来做分支和合并分支,管理生产版本
最近由于公司前端团队扩招,虽然小小的三四团队开发,但是也出现了好多问题.最让人揪心的是代码的管理问题:公司最近把版本控制工具从svn升级为git.前端H5组目前对git的使用还不是很熟悉,出现额多次覆 ...
- [转]ORA-28001: the password has expired解决方法
本文转自:http://blog.csdn.net/btt2013/article/details/54862420 参考文献:http://www.zhetao.com/content259 后台报 ...
- AspxGridView中行的双击事件
ClientSideEvents-RowDblClick="clike" function clike(s, e) { grdList.GetRowKey(e. ...
- Sqlserver 备份
Transact-SQL 语法规则 Transact-SQL 引用中的语法关系图使用下列规则. 大写 : Transact-SQL 关键字. 斜体 : Transact-S ...
- [javaSE] 单例设计模式
四人帮设计了23中设计模式 单例设计模式:解决一个类在内存中只存在一个对象 构造函数私有化 在类中创建一个本类对象 提供一个方法可以获取该对象 class Single{ private static ...