ios UISearchDisplayController 实现 UITableView 搜索功能
UISearchDisplayController 是苹果专为 UITableView 搜索封装的一个类。
里面内置了一个 UITableView 用于显示搜索的结果。它可以和一个需要搜索功能的
controller 关联起来,其它的像原 TableView 和搜索结果 TableView 的切换, mask 的显示等等都
封装好了,使用起来非常非常的简单。特别是要实现全屏搜索时使用最多。
全屏搜索的意思是如果你用了 NavigationBar 当点击搜索框时 TableView 会自动弹上去覆盖
NavigationBar,达到一种全屏搜索的效果,这一切 UISearchDisplayController 都封装好了,如果自己
写就比较麻烦一些。

关键代码:
@interface MainViewController : UITableViewController{
NSArray *data;
NSArray *filterData;
UISearchDisplayController *searchDisplayController;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width
, )];
searchBar.placeholder = @"搜索"; // 添加 searchbar 到 headerview
self.tableView.tableHeaderView = searchBar; // 用 searchbar 初始化 SearchDisplayController
// 并把 searchDisplayController 和当前 controller 关联起来
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; // searchResultsDataSource 就是 UITableViewDataSource
searchDisplayController.searchResultsDataSource = self;
// searchResultsDelegate 就是 UITableViewDelegate
searchDisplayController.searchResultsDelegate = self;
}
/*
* 如果原 TableView 和 SearchDisplayController 中的 TableView 的 delete 指向同一个对象
* 需要在回调中区分出当前是哪个 TableView
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.tableView) {
return data.count;
}else{
// 谓词搜索
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains [cd] %@",searchDisplayController.searchBar.text];
filterData = [[NSArray alloc] initWithArray:[data filteredArrayUsingPredicate:predicate]];
return filterData.count;
}
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"mycell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId]; if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
} if (tableView == self.tableView) {
cell.textLabel.text = data[indexPath.row];
}else{
cell.textLabel.text = filterData[indexPath.row];
} return cell;
}
DEMO 下载:http://pan.baidu.com/s/1pJ8vvC3
ios UISearchDisplayController 实现 UITableView 搜索功能的更多相关文章
- IOS UISearchDisplayController 点击搜索出现黑条问题解决方案
最近项目遇到一个很奇葩的问题 点击按钮启动 presentViewController 的时候出现下图效果: 代码: AddFriendViewController *addFriendVC = [[ ...
- iOS--- UITableView + UISearchDisplayController - - - - -实现搜索功能
iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 ...
- iOS8 UISearchViewController搜索功能讲解 分类: ios技术 2015-07-14 10:23 76人阅读 评论(0) 收藏
在iOS8以前我们实现搜索功能需要用到UISearchbar和UISearchDisplayController, 在iOS8之后呢, UISearchController配合UITableView的 ...
- iOS 模糊、精确搜索匹配功能方法总结 By HL
字符串搜索主要用于UITableView的搜索功能的筛选,过滤,查询 下面是一些流行的搜索查询方法 一.遍历搜索 for循环 根据要求:精确搜索(判读字符串相等) 模糊搜索(字符串包含) 相关知识 ...
- ios UITableView 搜索
自己实现 UITableView 搜索,相对于使用 UISearchDisplayController 来说自己写稍微麻烦了那么一点点,但是更加灵活.主要就是用一个字段区分出当前是搜索还是非搜索,然后 ...
- iOS开发系列--UITableView全面解析
--UIKit之UITableView 概述 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是U ...
- iOS8 UISearchViewController搜索功能讲解
在iOS8以前我们实现搜索功能需要用到UISearchbar和UISearchDisplayController, 在iOS8之后呢, UISearchController配合UITableView的 ...
- 简单实现UITableView索引功能(中英文首字母索引) (二) By HL
简单实现UITableView索引功能(中英文首字母索引)(一) ByH罗 相关类: NSString+PinYing(获取中英文首字母) 参考上面链接 #import "ViewCon ...
- [Android分享] 【转帖】Android ListView的A-Z字母排序和过滤搜索功能
感谢eoe社区的分享 最近看关于Android实现ListView的功能问题,一直都是小伙伴们关心探讨的Android开发问题之一,今天看到有关ListView实现A-Z字母排序和过滤搜索功能 ...
随机推荐
- nodejs版本升级
网上都说 npm install –g n 可是一直不行,换做 npm install -g cnpm --registry=https://registry.npm.taobao.org ok
- Delphi 按Esc快捷键退出程序的简单方法
第一种方法: 在窗体上放一个按钮: 1>.设置按钮的Cancel属性为True: 2>.在按钮的点击事件中写: procedure TForm1.btn1Click(Sender: TO ...
- jquery实现简单抽奖功能
一直纠结要怎么用jquery实现抽奖功能,看别人很多都是用flash制作的,找了很多资料,最终找到一个比较适合需求的,我做了些许调整,以下是代码展示(复制下来可以直接使用). 先上图:
- RobotFramework做接口自动化(post请求)
接口成功时返回: { "reCorde": "SUCCESS", "data": { ", "verify": ...
- 【项目总结】扯一扯电商网站前端css的整体架构设计(1)
最近半忙不忙的写了一个外包网站,网站主要功能是艺术品竞拍和艺术衍生品的销售.工程已经完成了80%左右,现在前后端代码量已经50W行左右,我主要负责的是前端设计和前端布局.下面就先放一个网站的设计图吧, ...
- 纸壳CMS(ZKEACMS)体验升级,快速创建页面,直接在页面中修改内容
关于纸壳CMS 纸壳CMS又名 ZKEACMS Core 是ZKEACMS的 .net core 版本,可运行在 .net core 1.1 平台上.是一个开源的CMS. 纸壳CMS对于 ZKEACM ...
- 如何使用jQuery写一个jQuery插件
jQuery插件其实是前端框架的思维,构成一个框架,个人认为必须满足以下几个基础条件:1. 可重用,2. 兼容性,3. 维护方便,虽说现在有很多比较成熟的前端框架,但是也有部分存在配置麻烦,学习成本大 ...
- coderfoces D. Gourmet choice
D. Gourmet choice time limit per test 2 seconds memory limit per test 256 megabytes 题目链接: https: ...
- 用Visual Studio 2015成功编译、发布UMDF驱动到目标机!!
开发工具:Visual Studio 2015企业版 主 机:windows10 X64企业版,主机是安装了Visual Studio 2015的操作系统,主要进行驱动开发和调试. 目 标 ...
- 2019中山大学程序设计竞赛(重现赛) Clumsy Keke
Problem Description Keke is currently studying engineering drawing courses, and the teacher has taug ...