最近在做一个简单的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简单使用的更多相关文章

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

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

  2. UISearchBar和 UISearchDisplayController的使用

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

  3. iOS 搜索框控件 最简单的dome

    刚学习搜索框控件,写了个最简单的dome #import <UIKit/UIKit.h> .h @interface ViewController : UIViewController&l ...

  4. UISearchController替换UISearchDisplayController

    随着iOS 的升级,iOS 7的占有率更低了.Xcode 升级到Xcode 8之后,对iOS 应用支持的最低版本,iOS 7也被抛弃了.我在新项目中也是最低支持到iOS 8,因此工程里也是各种警告.首 ...

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

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

  6. ios UISearchDisplayController 实现 UITableView 搜索功能

    UISearchDisplayController 是苹果专为 UITableView 搜索封装的一个类. 里面内置了一个 UITableView 用于显示搜索的结果.它可以和一个需要搜索功能的 co ...

  7. 简单的TableView

    背景知识 每个表都是UITableView的实例,表中的每一行都是UITableViewCell的实例. TableView的种类 Grouped table Plain table without ...

  8. ios UI控件的简单整理

    把该文件拷贝到.m文件中就能够方便的查找 /** 匿名类目:能够声明方法和变量,属性为private(不同意在外部调用,且不能被继承 */ /** 发送数据的托付方,接收数据的时代理发(即代理的反向传 ...

  9. 简单实现UITableView索引功能(中英文首字母索引) (二) By HL

    简单实现UITableView索引功能(中英文首字母索引)(一) ByH罗 相关类: NSString+PinYing(获取中英文首字母)   参考上面链接 #import "ViewCon ...

随机推荐

  1. [Oracle] 浅谈Sequence

    Oracle的Sequence是一种数据库对象,它可以生成有序数字,主要用于主键的自动生成.如果没有Sequence,主键的自动生成必须得在代码逻辑里实现,大致过程是:获取当前主键值,新主键值=当前主 ...

  2. angular ng-bind-html 对src路径失效 解决方案

    json内容 ;<img src="/newsfile/1506271512489.jpg" width="600" height="450&q ...

  3. php简单的爬虫

    爬虫的原理是分析下载的页面,找出其中的连接,然后再下载这些链接,对链接再进行更深层次的递归,周而复始.在数据存储方面,先存储到redis里面,再有redis 写入到mysql,这样可以减轻mysql写 ...

  4. IIS下 Yii Url重写

    下载URL重写组件 http://www.microsoft.com/zh-cn/download/details.aspx?id=7435 导入官方提供的.htaccess文件 Options +F ...

  5. pubwin数据云备份

    由于pubwin自带的异地备份一直不好用,并且pubwin自带的37分钟备份也不方便手动备份,考虑用python 与写一个基于酷盘的pubwin数据备份工具(本来想基于百度云的,发现百度云用的人太多, ...

  6. Linux方面收藏的一点儿资料

    初来乍到,也算是第一次写技术类相关的博客,就分享几篇收藏的Linux相关的资料吧,希望可以给需要的人一点帮助. 1.<高级Bash脚本编程指南>:该网站详细讲解了Bash Shell编程的 ...

  7. vsftpd允许root用户登录

    Linux下安装vsftpd之后,默认的配置是 匿名用户可以登录,匿名帐户有两个: 用户名:anonymous 密码:空 用户名:ftp 密码:ftp 如果要用匿名进行上传删除等操作需要配置其它参数. ...

  8. AIX和Linux中wtmp的不同处理方式

    wtmp 记录用户登录和退出事件.它和utmp日志文件相似,但它随着登陆次数的增加,它会变的越来越大,有些系统的ftp访问也在这个文件里记录,同时它也记录正常的系统退出时间,可以用ac和last命令访 ...

  9. android Service简介及启动关闭方式

    (1)Service是Android系统中的四大组件之一,和Activity是同一层次的组件:它是一种生命周期较长,没有可视化界面,运行于后台的一种服务:例如,我们听音乐可以使用Service,下载东 ...

  10. 动态规划——min/max的单调性优化总结

    一般形式: $max\{min(ax+by+c,dF(x)+eG(y)+f)\},其中F(x)和G(y)是单调函数.$ 或 $min\{max(ax+by+c,dF(x)+eG(y)+f)\},其中F ...