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

  1. iOS--- UITableView + UISearchDisplayController - - - - -实现搜索功能

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

  2. iOS--UISearchBar和UISearchDisplayController

    UISearchBar继承自UIView.UIResponder.NSObject 属性: autocapitalizationType————自动对输入文本对象进行大小写设置(包含4种类型,但是有时 ...

  3. UISearchBar和 UISearchDisplayController的使用

    感觉好多文章不是很全面,所以本文收集整合了网上的几篇文章,感觉有互相补充的效果. 如果想下载源码来看:http://code4app.com/search/searchbar .本源码与本文无关 1. ...

  4. UISerachBar / UISearchDisplayController

    1. UISerachBar 继承与UIView, 包含uitextfield, 并且实现了uitextfielddelegate代理的主要内容 含有取消按钮, 默认不显示 2. UISerachDi ...

  5. 如何在UINavigationBar上添加UISearchBar以及UISearchDisplayController的使用 --OC --iOS

    那我们开始吧,下面是Sely写的一个Demo,分享给大家. 新建一个项目, UISearchDisplayController 的 displaysSearchBarInNavigationBar太死 ...

  6. UISearchDisplayController简单使用

    最近在做一个简单的app入门,中间有一个页面用到了搜索框,本来以为很简单的控件,没想到用到的时候才发现很麻烦. 搜索框使用过程大约有以下几个状态:不活跃-活跃-输入关键词-根据关键词动态列出相关结果- ...

  7. iOS 用UISearchDisplayController实现查找功能

    UISearchDisplayController是iOS中用于处理搜索功能的控制器,此控制器需要和UISearchBar结合使用 示例代码如下: // // WKRootViewController ...

  8. Swift - 带结果列表的搜索条(UISearchDisplayController)的用法

    (注:自iOS8起,苹果便废弃UISearchDisplayController的使用,改为使用UISearchController来实现类似功能,可参考我的另一篇文章“Swift - 使用UISea ...

  9. 点击搜索取消UISearchDisplayController的搜索状态

    一般,我们用到UISearchDisplayController的时候,都是须要对一个数据源进行刷选,在UISearchDisplayController自带的tableView中展示出来,然后点击退 ...

  10. iOS UISearchDisplayController学习笔记

    UISearchDisplayController和UISearchBar一起使用用来管理UISearchBar和搜索结果的展示.UISearchDisplayController提供了显示搜索结果的 ...

随机推荐

  1. tomcat启动(二)org.apache.catalina.startup.Bootstrap分析

    /** * Bootstrap loader for Catalina. This application constructs a class loader * for use in loading ...

  2. Pelican+Github博客搭建详细教程

    操作系统:Mac OS / Linux 工具集: 1.Pelican--基于Python的静态网页生成器 2.马克飞象--Evernote出的Markdown文本编辑器 3.GoDaddy--域名供应 ...

  3. springboot 常用插件

    热部署 使用run as -java application, 把spring-loader-1.2.4.RELEASE.jar下载下来,放到项目的lib目录中,然后把IDEA的run参数里VM参数设 ...

  4. Linux-(kill,wc,killall,ln,cal,date)

    kill命令 1.命令格式: kill [参数] [进程号] 2.命令功能: 发送指定的信号到相应进程.不指定型号将发送SIGTERM(15)终止指定进程.如果仍无法终止该程序可用“-KILL” 参数 ...

  5. 在Ubuntu16.04上使用Open Grok

    Open Grok是一个强大的源代码搜索和对照引擎,是Open Solaris的源文件浏览及搜索工具.虽然Open Solaris已经不复存在,但这个工具仍然处于开发与维护之中. 相信我,绝对值得你拥 ...

  6. goldarch企业管理软件框架整体解决方案终于出来了

    所有的图片及解决方案都在我的博客里http://blog.posn.net 框架把企业管理软件开发中要遇到的常用做了组件化处理,达到了通用性和可定制性的目的. goldarch的数据层是spring. ...

  7. 基于Java的简易表达式解析工具(二)

    之前简单的介绍了这个基于Java表达式解析工具,现在把代码分享给大家,希望帮助到有需要的人们,这个分享代码中依赖了一些其他的类,这些类大家可以根据自己的情况进行导入,无非就是写字符串处理工具类,日期处 ...

  8. LVS负载均衡DR模式部署

    目录: 1. 拓扑图 2. 搭建环境 3. LVS服务器部署 4. 测试 1. 拓扑图     LVS-DR模式采的IP地址全部为外网IP.    本例中IP的设置全部采用临时设置IP的方式,重启后会 ...

  9. MongoDB之数据库管理

    前面补充过把MongoDB作为服务,今天主要了解数据库管理,在操作之前先把MongoDB服务打开. 一.显示数据库清单 如果想查看数据库列表可以使用show dbs. 二.切换数据库 通过使用Mong ...

  10. web前端--实现前后端分离的心得

    1.实现前后端分离的心得 2.前后端分离实践 3.谈谈前后端的分工协作 4.从MVC到前后端分离(REST-个人也认为是目前比较流行和比较好的方式) 4.1.REST风格框架实战:从MVC到前后端分离 ...