最近在做一个简单的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. struts2 JS获取上传文件的绝对路径,兼容IE和FF

    因为file控件上传失败后会自动清空,所以使用文本框来保存上传路径,而且在不同的浏览器下,控件的样式也需要兼容.下面是自己用到的实例 // 初始化判断浏览器的版本,根据版本的不同使用不同的样式func ...

  2. POJ3484 Showstopper (二分+字符串处理)

    POJ3484 Showstopper 题目大意: 每次给出三个数x,y,z,用这三个数构成一个等差数列,x为首项,y是末项,z是公差 总共给出n组x,y,z( n待定),求这n组数列中出现次数为奇数 ...

  3. 你好,C++(19)“老师,我这次四级考试过了没有?”——4.2 条件选择语句

    4.2  条件选择语句 “老师,我这次四级考试过了没有?” 如果老师被问到这个问题,他会如何回答?是的,他会根据不同的条件选择不同的回答: 如果考试成绩大于等于60,那就回答:“恭喜你,你通过了这次考 ...

  4. WinForm(C#)自定义控件之——RoundButton(圆形按钮)

    最近需要做一个圆形的按钮,去CodeProject找了一下,发现有现成的可用,但不能完全满足我的需求.因此自己试着利用WinForm中的自定义组件功能,制作一个圆形按钮.圆形按钮小制作即将开始之前,先 ...

  5. 对于js原型和原型链继承的简单理解(第二种,对象冒充)

    关键代码    this.parent = Cat;    this.parent.apply(this); function Cat(){ this.climb = function(){ aler ...

  6. windows搭建redis记录

    windows安装redis:http://www.cnblogs.com/linjiqin/archive/2013/05/27/3101694.html 30个常用的redis命令:http:// ...

  7. C语言格式化输入输出函数

    一:格式输出函数printf() 1.调用形式一般为:printf("格式化控制字符串",输出表列): 2.格式化控制字符串用于指定输出格式,它有三种形式: 1.格式说明符:规定了 ...

  8. Java Tips: 使用Pattern.split替代String.split

    String.split方法很常用,用于切割字符串,split传入的参数是正则表达式,它的内部是每次都comiple正则表达式,再调用Pattern.split方法: public String[] ...

  9. Robot FrameWork 教程链接

    1.  Robot Framework 教程: http://cgmblog.sinaapp.com/html/category/robot-framework 2.  Robot Framework ...

  10. java练习-滚动文字

    <marquee direction="left" onMouseOver="this.scrollAmount=5" onMouseOut=" ...