iOS 用UISearchDisplayController实现查找功能
UISearchDisplayController是iOS中用于处理搜索功能的控制器,此控制器需要和UISearchBar结合使用
示例代码如下:
//
// WKRootViewController.m
// 表格视图的搜索功能
//
// Created by student on 14-10-20.
// Copyright (c) 2014年 wukong. All rights reserved.
// #import "WKRootViewController.h" @interface WKRootViewController () @property (strong, nonatomic) NSMutableArray* dataSource; @property (strong, nonatomic)NSMutableArray* resultArrat; @end @implementation WKRootViewController
{
//用于加载数据源的表视图
UITableView *_tableView; UISearchBar *_searchBar; UISearchDisplayController *_searchControl;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad]; [self createUI];
[self createDataSource];
// Do any additional setup after loading the view.
} - (void)createDataSource
{
_dataSource = [[NSMutableArray alloc] init];
_resultArrat = [[NSMutableArray alloc] init];
for (int i = 'A'; i <= 'z'; i++) {
NSMutableArray *section = [[NSMutableArray alloc] init];
for (int j = ; j <= ; j++) {
NSString *str = [NSString stringWithFormat:@"%c-%d", i, j];
[section addObject:str];
}
[_dataSource addObject:section];
}
} #pragma mark- UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//判断当前展示的表格
if (tableView != _tableView)
return ;
return _dataSource.count;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView != _tableView) {
return _resultArrat.count;
}
return [[_dataSource objectAtIndex:section] count];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (tableView != _tableView) {
cell.textLabel.text = [_resultArrat objectAtIndex:indexPath.row];
}else{
cell.textLabel.text = [[_dataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
return cell;
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark - CreateUI
- (void)createUI
{
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(, , , ) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView]; _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(, , , )];
_searchBar.searchBarStyle = UISearchBarStyleMinimal;
_searchBar.delegate = self;
[_tableView setTableHeaderView:_searchBar];
/*
第一个参数:用于输入搜索内容的UISearchBar对象
第二个参数:提供给我的表格视图数据源的控制器对象,这个对象必须是实现了表格的两个协议
*/
_searchControl = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
// _searchControl.searchResultsTableView
// UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
// label.backgroundColor =[UIColor redColor];
// [_searchControl.searchResultsTableView setTableHeaderView:label];
//设置_searchControl自带的表格视图的委托对象
[_searchControl setSearchResultsDataSource:self];
[_searchControl setSearchResultsDelegate:self];
} #pragma mark -UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[_resultArrat removeAllObjects];
NSString *str = [NSString stringWithFormat:@"*%@*", searchText];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF like %@", str];
for (NSMutableArray *arr in _dataSource) {
for (NSString *str in arr) {
if ([pred evaluateWithObject:str]) {
[_resultArrat addObject:str];
}
}
}
}
@end
iOS 用UISearchDisplayController实现查找功能的更多相关文章
- iOS--- UITableView + UISearchDisplayController - - - - -实现搜索功能
iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 ...
- 在你的ASP.NET MVC中使用查找功能
在程序中,使用查找功能是少之不了.今天在ASP.NET环境下演示一回. 在cshtml视图中,有三个文本框,让用户输入关键词,然后点击最右连的“搜索”铵钮,如果有结果将显示于下面. Html: 表格放 ...
- 改进iOS客户端的升级提醒功能
改进iOS客户端的升级提醒功能 功能设计 先申明一下,我是码农,不是一个产品经理,但我觉得现有市面上的很多 App,设计的 "升级提示功能" 都不太友好.在此分享一下我的想法,欢迎 ...
- JS实现项目查找功能
又是好久没有更新文章了,技术差,人又懒是重罪啊!! 在工作中每天都要查找目前正在接手的项目,而如果项目一多起来怎么办呢? 最近主管突然说要找一下以前的项目改一点BUG,然后我就找了半天才找到对应的文件 ...
- Dynamics CRM2015 页面导航栏顶部全局快速查找功能配置
在CRM2015中微软加入了新的快速查找功能,让你的数据查找更加方便,功能栏如下图所示,直接可以框中输入搜索项进行搜索. 但该功能是需要进行些配置,具体的配置在设置-管理-系统设置中,默认的就是红框中 ...
- vim之快速查找功能
vim有强大的字符串查找功能. 我们通常在vim下要查找字符串的时候, 都是输入 / 或者 ? 加 需要查找的字符串来进行搜索,比如想搜索 super 这个单词, 可以输入 /super 或者 ...
- vi中换行、翻页和查找功能
vi时,按ESC进入Command模式, 1. 移动光标命令:j 向下移动一行:k 向上移动一行:h 向左移动一个字符:l 向右移动一个字符:对于 j.k.l和h键,命令前加数字,移动多行:如 3j, ...
- iOS Swift WisdomScanKit图片浏览器功能SDK
iOS Swift WisdomScanKit图片浏览器功能SDK使用 一:简介 WisdomScanKit 由 Swift4.2版编写,完全兼容OC项目调用. WisdomScanKit的 ...
- 使用vs的查找功能,简单大概的统计vs中的代码行数
VS强大的查找功能,可以使用正则表达式来进行查找,这里统计代码行数的原理就是: 在所有指定文件中进行搜索,统计匹配的文本行数. 但是匹配的行需要满足:非注释.非空等特殊非代码行. 使用Ctrl+Shi ...
随机推荐
- 转:web前端面试题合集 (Javascript相关)(js异步加载详解)
1. HTTP协议的状态消息都有哪些? 1**:请求收到,继续处理2**:操作成功收到,分析.接受3**:完成此请求必须进一步处理4**:请求包含一个错误语法或不能完成5**:服务器执行一个完全有效请 ...
- Windows Azure Web Role 的 IIS 重置
如果您是一名 Web开发人员,您很可能使用过"简单快捷"的iisreset命令重置运行不正常的 IIS主机.这种方法通常在经典的 Windows Server VM上非常有效 ...
- python第三天---collections类
collection系列 1.计数器(counter) Counter是对字典类型的补充,用于追踪值的出现次数. 我们从中挑选一些相对常用的方法来举例: 在上面的例子我们可以看出,counter方法返 ...
- poj 3680 Intervals(费用流)
http://poj.org/problem?id=3680 巧妙的构图. 题目:给定N个区间(ai,bi)权值wi,求最大权和且每个点最多覆盖K次. 构图:将区间端点离散化,将第i个点连第i+1个点 ...
- Sublime Text 3:3114的安装(目前最新),插件emmet的安装
随便一些好了. 直接英文版吧,建议不要找中文版,学习英语不是? https://www.sublimetext.com/3 下载 https://github.com/wbond/package_ ...
- JavaScript引用类型之Array数组的栈方法与队列方法
一.栈方法 ECMAScript数组也提供了一种让数组的行为类似与其他数据结构的方法.具体的来说,数组可以变现的向栈一样,栈就是一种可以限制插入和删除向的数据结构.栈是一种LIFO(Last In F ...
- ASPxComboBox-通过回车过滤结果集
Dev ASP.NET组件中的ASPxComboBox可以方便的根据输入内容进行过滤,不过对于数据量较大或者用户数较多的情况下,这个功能会给服务器带来严重的负担,因此我们应该输入自己想要查询的字符串时 ...
- js中字符串方法
字符串方法: 1. charAt(索引值)//通过索引值获取字符串中对应的值 例如: var str='sdf123'; alert(str.charAt(0));//结果弹出第一个索引对应的值:s
- 红豆带你从零学C#系列之:初识继承与多态
继承 现实生活当中,人类又可以根据职业分为:教师,学生,理发师,售货员 又比如飞机又有种类之分:直升飞机.客机.货机.战斗机等 在程序里面我们可能会通过创建类来描述这样的事物,比如学生类.教师类.理发 ...
- Hadoop学习之HBase和Hive的区别
Hive是为简化编写MapReduce程序而生的,使用MapReduce做过数据分析的人都知道,很多分析程序除业务逻辑不同外,程序流程基本一样.在这种情况下,就需要Hive这样的用户编程接口.Hive ...