UISearchDisplayController简单使用
最近在做一个简单的app入门,中间有一个页面用到了搜索框,本来以为很简单的控件,没想到用到的时候才发现很麻烦。

搜索框使用过程大约有以下几个状态:不活跃-活跃-输入关键词-根据关键词动态列出相关结果-选中搜索结果或取消搜索
// ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate>
//相关协议 @property (strong, nonatomic) NSMutableArray *dataList;//搜索数据源
@property (strong, nonatomic) NSMutableArray *searchList;//搜索结果 @property (strong, nonatomic) UITableView *tableView;//数据源tableview @end
// ViewController.m
#import "ViewController.h" @interface ViewController () @property (nonatomic) UISearchDisplayController *searchDisplayController; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.dataList = [NSMutableArray arrayWithCapacity:];
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(, , self.view.bounds.size.width, self.view.bounds.size.height - )];
for (NSInteger i = ; i < ; i++) {
[self.dataList addObject:[NSString stringWithFormat:@"%ld-yang",i]];
} self.tableView.delegate = self;
self.tableView.dataSource = self; UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(, , self.view.bounds.size.width, )];
[searchBar setPlaceholder:@"关键字搜索"];
searchBar.delegate = self; [self.view addSubview:searchBar]; self.searchDisplayController = [[UISearchDisplayController alloc]initWithSearchBar:searchBar contentsController:self]; _searchDisplayController.delegate = self; [_searchDisplayController.searchResultsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
_searchDisplayController.searchResultsTableView.dataSource = self;
_searchDisplayController.searchResultsTableView.delegate = self; [self.view addSubview:self.tableView]; }- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [self.searchList count];
}else{
return [self.dataList count];
}
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
if (tableView == self.tableView){//判断当前tableview是否为搜索结果
[cell.textLabel setText:self.dataList[indexPath.row]];
}else{
[cell.textLabel setText:self.searchList[indexPath.row]];
} return cell;
}
//协议方法:开始搜索
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
NSLog(@"search begin");
searchBar.frame = CGRectMake(, , , );
[self.searchDisplayController setActive:YES];
return YES;
}
//协议方法:搜索结束
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
NSLog(@"search end");
searchBar.frame = CGRectMake(, , , );
[self.searchDisplayController setActive:NO];
return YES;
}
//关键词变化时实时更新搜索结果
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c]%@",searchString];
//清空结果数组
[self.searchList removeAllObjects]; //过滤数据
NSArray *a = [_dataList filteredArrayUsingPredicate:preicate];
self.searchList = [NSMutableArray arrayWithArray:a]; //刷新表格
return YES;
} @end
UISearchDisplayController简单使用的更多相关文章
- iOS--- UITableView + UISearchDisplayController - - - - -实现搜索功能
iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 ...
- UISearchBar和 UISearchDisplayController的使用
感觉好多文章不是很全面,所以本文收集整合了网上的几篇文章,感觉有互相补充的效果. 如果想下载源码来看:http://code4app.com/search/searchbar .本源码与本文无关 1. ...
- iOS 搜索框控件 最简单的dome
刚学习搜索框控件,写了个最简单的dome #import <UIKit/UIKit.h> .h @interface ViewController : UIViewController&l ...
- UISearchController替换UISearchDisplayController
随着iOS 的升级,iOS 7的占有率更低了.Xcode 升级到Xcode 8之后,对iOS 应用支持的最低版本,iOS 7也被抛弃了.我在新项目中也是最低支持到iOS 8,因此工程里也是各种警告.首 ...
- iOS中的两种搜索方式UISearchDisplayController和UISearchController
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 以前iOS的搜索一般都使用UISearchDisplayCon ...
- ios UISearchDisplayController 实现 UITableView 搜索功能
UISearchDisplayController 是苹果专为 UITableView 搜索封装的一个类. 里面内置了一个 UITableView 用于显示搜索的结果.它可以和一个需要搜索功能的 co ...
- 简单的TableView
背景知识 每个表都是UITableView的实例,表中的每一行都是UITableViewCell的实例. TableView的种类 Grouped table Plain table without ...
- ios UI控件的简单整理
把该文件拷贝到.m文件中就能够方便的查找 /** 匿名类目:能够声明方法和变量,属性为private(不同意在外部调用,且不能被继承 */ /** 发送数据的托付方,接收数据的时代理发(即代理的反向传 ...
- 简单实现UITableView索引功能(中英文首字母索引) (二) By HL
简单实现UITableView索引功能(中英文首字母索引)(一) ByH罗 相关类: NSString+PinYing(获取中英文首字母) 参考上面链接 #import "ViewCon ...
随机推荐
- git 笔记记录
分布式版本控制系统Git 是一套内容寻址文件系统,从核心上来看不过是简单地存储键值对.一: git 本地clone 一个仓库 1. 直接clone一个仓库: $: git clon ...
- smarty练习:数据的增删改
根据数据库中的三张表格:timu,xuanxiang,kemu来进行数据的增删改查,并且使用smarty模版将前端与后台分离开来 三张表格: 主页面后台 main.php: <?php //引入 ...
- 3月23日html(五) 格式与布局练习:360浏览器布局
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < ...
- Apache配置rewrite
最近将代码做了迁移,更换了web服务器,从原来的Nginx,换成使用Apache,多少有些区别.这里整理一下在apache下实现rewrite功能. 第一部分:修改apache配置文件支持rewrit ...
- Kafka笔记--参数说明及Demo
参考资料:http://blog.csdn.net/honglei915/article/details/37563647参数说明:http://ju.outofmemory.cn/entry/119 ...
- Thinking in Java——笔记(21)
Concurrency However, becoming adept at concurrent programming theory and techniques is a step up fro ...
- 对于Android的线程和线程池的理解
Android的消息机制,主要是指Handler的运行机制,Handler的运行需要底层的MessageQueue 和 Looper的支撑,MessageQueue中文名消息队列,它的内部存储了一组消 ...
- Gson 禁止特殊字符转码
Map<String,String> map = new HashMap<String,String>(); map.put("url", "&a ...
- Oracle 存储过程,临时表,动态SQL测试
--创建事务级别的结果临时表 create global temporary table tmp_yshy( c1 ), c2 ) )on commit delete rows; --创建事务级别的存 ...
- android 让图片充满整个屏幕
方法1: <ImageView android:id="@+id/imageView1" android:layout_width="fill_parent&quo ...