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实现查找功能的更多相关文章

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

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

  2. 在你的ASP.NET MVC中使用查找功能

    在程序中,使用查找功能是少之不了.今天在ASP.NET环境下演示一回. 在cshtml视图中,有三个文本框,让用户输入关键词,然后点击最右连的“搜索”铵钮,如果有结果将显示于下面. Html: 表格放 ...

  3. 改进iOS客户端的升级提醒功能

    改进iOS客户端的升级提醒功能 功能设计 先申明一下,我是码农,不是一个产品经理,但我觉得现有市面上的很多 App,设计的 "升级提示功能" 都不太友好.在此分享一下我的想法,欢迎 ...

  4. JS实现项目查找功能

    又是好久没有更新文章了,技术差,人又懒是重罪啊!! 在工作中每天都要查找目前正在接手的项目,而如果项目一多起来怎么办呢? 最近主管突然说要找一下以前的项目改一点BUG,然后我就找了半天才找到对应的文件 ...

  5. Dynamics CRM2015 页面导航栏顶部全局快速查找功能配置

    在CRM2015中微软加入了新的快速查找功能,让你的数据查找更加方便,功能栏如下图所示,直接可以框中输入搜索项进行搜索. 但该功能是需要进行些配置,具体的配置在设置-管理-系统设置中,默认的就是红框中 ...

  6. vim之快速查找功能

    vim有强大的字符串查找功能. 我们通常在vim下要查找字符串的时候, 都是输入 / 或者 ?  加 需要查找的字符串来进行搜索,比如想搜索 super 这个单词, 可以输入  /super  或者 ...

  7. vi中换行、翻页和查找功能

    vi时,按ESC进入Command模式, 1. 移动光标命令:j 向下移动一行:k 向上移动一行:h 向左移动一个字符:l 向右移动一个字符:对于 j.k.l和h键,命令前加数字,移动多行:如 3j, ...

  8. iOS Swift WisdomScanKit图片浏览器功能SDK

    iOS Swift WisdomScanKit图片浏览器功能SDK使用 一:简介      WisdomScanKit 由 Swift4.2版编写,完全兼容OC项目调用. WisdomScanKit的 ...

  9. 使用vs的查找功能,简单大概的统计vs中的代码行数

    VS强大的查找功能,可以使用正则表达式来进行查找,这里统计代码行数的原理就是: 在所有指定文件中进行搜索,统计匹配的文本行数. 但是匹配的行需要满足:非注释.非空等特殊非代码行. 使用Ctrl+Shi ...

随机推荐

  1. 强大的DELPHI RTTI–兼谈需要了解多种开发语言

    一月 27th, 2005 by 猛禽 风焱在<“18般武艺”?>中说到他碰上的被多种语言纠缠的问题.我在回复里说: 很多语言只要能看懂几分就行了,没必要每一种都精通 但是如果只会很少的一 ...

  2. firemonkey打开子窗体

    procedure TForm1.Button1Click(Sender: TObject);varChildForm: TForm2;beginChildForm := TForm2.Create( ...

  3. git Feature分支

    Feature分支 软件开发中,总有无穷无尽的新的功能要不断添加进来. 添加一个新功能时,你肯定不希望因为一些实验性质的代码,把主分支搞乱了,所以,每添加一个新功能,最好新建一个feature分支,在 ...

  4. NOI2014 Day1

    NOI2014 Day1 起床困难综合症 题目描述:给出\(n\)个操作,每个操作包含一种运算\((XOR.OR.AND)\)和一个数,已知一开始的数字为\([0, m]\),问经过\(n\)次操作后 ...

  5. NOI2013 Day2

    NOI2013 Day2 矩阵游戏 题目描述:设矩阵\(F\) 求\(F[n][m](mod (10^9+7))\) solution: 这题可以求通项解决. 设\(X_i=F[i][m]\), \( ...

  6. c语言函数---M

    书画小说软件 制作更满意的读.更舒心的写.更轻松的公布 最全古典小说网 由本软件公布所得 main()主函数 每一C 程序都必须有一main()函数, 能够依据自己的爱好把它放在程序的某 个地方.有些 ...

  7. C#高级编程三十天----泛型结构,泛型方法,泛型托付

    泛型结构 泛型结构和泛型类差点儿是一直的,仅仅是泛型结构没有继承的特性..NET平台提供的一个泛型结构是(可空类型)Nullablle<T>.可空类型的引入,主要是为了解决数据库语言中的数 ...

  8. Spoken English

    The Business lunch 9.商务午餐怎样开场?[0731] Is this your first time in shanghai? 10.怎样询问对方的感受?[0801] How do ...

  9. 从VS转MyEclipse的15天使用体验

    脱离了VS强大的IDE功能之后,转向MyEclipse,发现很大差别,Java的IDE对比VS感觉弱很多,而且树形没有那么好用,Java里面是以包为主,区别与C#的最大就是,高亮提示关键字,这一点Ja ...

  10. 浅谈Spring(四)

    一.Spring+MyBatis整合 spring大大简化了Mybatis的开发步骤. 1.MyBatis的开发要点: mybatis-config.xml配置文件:配置与数据库的链接.mapper文 ...