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 ...
随机推荐
- Swift中扩展的使用
import Foundation /* 扩展 1.使用扩展添加属性, 方法, 可变方法, 构造器, 下标, 嵌套类型 2.可以使一个已有类型符合一个或者多个协议 3.扩展与OC的Category类似 ...
- PL/SQL Developer主界面窗口左边窗口默认设置
中文版:在菜单 工具 -> 首选项 -> 用户界面 -> 选项 窗口中,将“自动保存桌面”勾选上就可以了. 截图如下: 英文版:在菜单 Tools -> Preferences ...
- Ghost克隆软件
克隆软件Ghost初级使用教程 一.什么是Ghost ? Ghost(幽灵)软件是美国赛门铁克公司推出的一款出色的硬盘备份还原工具,可以实现FAT16.FAT32.NTFS.OS2等多种硬盘分区格式的 ...
- Mysql学习(慕课学习笔记9)查询、分组
查找记录 Select select username,id from users; Group by 进行分组 select sex from users group by sex; 分组条件 se ...
- JS原型的剖析与理解
原型相关的概念 关于面向对象的概念 类 class 在js中就是构造函数 在传统的面向对象语言中,使用一个叫类的东西定义模版,然后使用模版创建对象 在构造方法中也具有类似的功能,因此称其为类 实例与对 ...
- /dev/socket/vold exploit 本地提权漏洞
EXPLOIT "0 asec create ../../../../../../../../xxxxx/xx/xx/xx 1 ext4 98235792350852308254872354 ...
- git新手碰到的各种奇葩问题之一
git 操作错误: <1>.情景描述:当在git commit --amend 更新上一次提交时,而此时提交日志会跳转到别人的日志中.,会出现错误:如下 弥补操作: 1.git fetc ...
- 通过设计让APP变快的6个方法
我们都知道不管网页还是移动应用,响应速度都是最重要的体验指标之一,并且移动应用的网络环境不稳定,速度的体验显得尤为重要.其实速度优化不仅是程序员的事,设计,也能够让APP变得更快. 1. 后台执行 这 ...
- Codeforces 527D Clique Problem
http://codeforces.com/problemset/problem/527/D 题意:给出一些点的xi和wi,当|xi−xj|≥wi+wj的时候,两点间存在一条边,找出一个最大的集合,集 ...
- 从Lumia退役看为什么WP走向没落(从程序员与市场开发的角度,讲的真棒!)
http://www.cnblogs.com/zhangkai2237/p/4856880.html