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 搜索功能的更多相关文章

  1. IOS UISearchDisplayController 点击搜索出现黑条问题解决方案

    最近项目遇到一个很奇葩的问题 点击按钮启动 presentViewController 的时候出现下图效果: 代码: AddFriendViewController *addFriendVC = [[ ...

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

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

  3. iOS8 UISearchViewController搜索功能讲解 分类: ios技术 2015-07-14 10:23 76人阅读 评论(0) 收藏

    在iOS8以前我们实现搜索功能需要用到UISearchbar和UISearchDisplayController, 在iOS8之后呢, UISearchController配合UITableView的 ...

  4. iOS 模糊、精确搜索匹配功能方法总结 By HL

    字符串搜索主要用于UITableView的搜索功能的筛选,过滤,查询 下面是一些流行的搜索查询方法 一.遍历搜索 for循环 根据要求:精确搜索(判读字符串相等)   模糊搜索(字符串包含) 相关知识 ...

  5. ios UITableView 搜索

    自己实现 UITableView 搜索,相对于使用 UISearchDisplayController 来说自己写稍微麻烦了那么一点点,但是更加灵活.主要就是用一个字段区分出当前是搜索还是非搜索,然后 ...

  6. iOS开发系列--UITableView全面解析

    --UIKit之UITableView 概述 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是U ...

  7. iOS8 UISearchViewController搜索功能讲解

    在iOS8以前我们实现搜索功能需要用到UISearchbar和UISearchDisplayController, 在iOS8之后呢, UISearchController配合UITableView的 ...

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

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

  9. [Android分享] 【转帖】Android ListView的A-Z字母排序和过滤搜索功能

      感谢eoe社区的分享   最近看关于Android实现ListView的功能问题,一直都是小伙伴们关心探讨的Android开发问题之一,今天看到有关ListView实现A-Z字母排序和过滤搜索功能 ...

随机推荐

  1. DELPHI XE5/6/7 android 无线真机调试

    一.下载adbWireless 地址:http://sj.zol.com.cn/detail/41/40834.shtml 安装,需要ROOT权限. 运adbWireless.界面很简单,就一个大按钮 ...

  2. 优化体验之使用visual EDM之映射存储过程,datatype to Enum

    stored produce,datatype to Enum,Colored Entity,Multiple Diagrams 一:EDM给我们提供的强大功能 1. 存储过程的映射 直接灌sql到d ...

  3. ZKEACMS for .Net Core 深度解析

    ZKEACMS 简介 ZKEACMS.Core 是基于 .Net Core MVC 开发的开源CMS.ZKEACMS可以让用户自由规划页面布局,使用可视化编辑设计“所见即所得”,直接在页面上进行拖放添 ...

  4. 使用PLSQL Developer时中文乱码问题

    使用PLSQL Developer时中文乱码问题 一.问题: 执行一些查询结果有中文的SQL语句,显示不了中文,显示???. 二.产生的原因: 客户端与服务器端的编码不一致造成的. 三.解决方案: 1 ...

  5. Ubuntu16.04以root身份登入!

    首先以非root用户身份登入系统. 1,修改root密码:启动shell,随后在shell里面输入命令: sudo passwd root 最后输入root要使用的密码,需要输入两次,这样root密码 ...

  6. code::blocks学习(2)

    转自: http://blog.csdn.net/sinat_27088253/article/details/52137855 http://jingyan.baidu.com/article/0b ...

  7. linux kvm虚拟机安装

    1.上传ISO文件,这里采用OEL5.8x64iso 2.开始安装OEL5.8 (1)raw格式磁盘 virt- --vcpus= --disk path=/data/test02.img,size= ...

  8. ArrayList用法详解

    1.什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: 动态的增加和减少元素 实现了ICollection和ILis ...

  9. 获取请求 header 中指定字段的值

    private function getHeader($name) {//获取请求头中$name的值 $name = 'HTTP_' . $name; foreach ($_SERVER as $ke ...

  10. ArchLinux 下文件描述符

    stderr -> /proc/self/fd/2 标准错误:2 stdin -> /proc/self/fd/0 标准输入:0 stdout -> /proc/self/fd/1 ...