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. ARM Cortex M3(V7-M架构)硬件启动程序 一

    Cortex-m3启动代码分析笔记 启动代码文件名是STM32F10X.S,它的作用先总结下,然后再分析. 启动代码作用一般是: 1)堆和栈的初始化: 2)中断向量表定义: 3)地址重映射及中断向量表 ...

  2. 杭电 1272 POJ 1308 小希的迷宫

    这道题是我学了并查集过后做的第三个题,教我们的学姐说这是并查集的基础题,所以有必要牢牢掌握. 下面就我做这道题的经验,给大家一些建议吧!当然,我的建议不是最好的,还请各位大神指出我的错误来,我也好改正 ...

  3. 去英国Savile Row 做件私人定制手工西装_GQ男士网

    去英国Savile Row 做件私人定制手工西装_GQ男士网 去英国Savile Row 做件私人定制手工西装

  4. 网易云课堂_C语言程序设计进阶_第6周:程序结构

    6.1 全局变量 6.2 编译预处理 6.3 大程序结构 6.1 全局变量 全局变量 定义在函数外面的变量是全局变量 全局变量具有全局的生存期和作用域 它们与任何函数都无关 在任何函数内部都可以使用它 ...

  5. python开发的一些tips

    1. Notepad++编写python脚本 1)新建文件,编写代码 2)点击菜单栏,“语言”—>“P”—>“Python”,设置脚本为Python语言的高亮(这样保存文本的时候,Note ...

  6. JavaScript之面向对象学习四原型对象的动态性

    1.由于在原型中查找值的过程是一次搜索,因此我们对原型对象所做的任何修改都能够立即从实例上反映出来---即便是先创建了实例后修改原型也是如此.代码如下: function Person(){ } va ...

  7. ASPxGridView-为每行添加序号

    添加一个新的非绑定列,使用CustomColumnDisplayText事件来分配序号给该列 <dx:GridViewDataTextColumn Caption="序号" ...

  8. JDK源码学习--String篇(三) 存储篇

    在进一步解读String类时,先了解下内存分配和数据存储的. 数据存储 1.寄存器:最快的存储区,位于处理器的内部.由于寄存器的数量有限,所以寄存器是按需分配. 2.堆栈:位于RAM中,但是通过堆栈指 ...

  9. scanf函数和printf函数

    C程序实现输出和输入的 主要是printf函数 和 scanf函数,这两个函数是格式输入输出 格式声明由%和格式字符组成 如%d,%f 格式字符:  d格式符:用来输出一个有符号的十进制整数  c格式 ...

  10. Linux学习之Center os网络配置

    首先介绍一下VMware的网络连接方式.我们的主机通常经过物理网卡与局域网和Internet相连接,而虚拟机既已被称之为虚拟计算机,那虚拟网卡也必然存在,而如果我们想让虚拟计算机能连接到局域网或者In ...