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 ...
随机推荐
- CDC变更数据捕获
CDC变更数据捕获 (2013-03-20 15:25:52) 分类: SQL SQL Server中记录数据变更的四个方法:触发器.Output子句.变更数据捕获(Change Data Cap ...
- CF卡是什么
CF卡(Compact Flash)最初是一种用于便携式电子设备的数据存储设备.作为一种存储设备,它革命性的使用了闪存,于1994年首次由SanDisk公司生产并制定了相关规范.当前,它的物理格式已经 ...
- Android下pm 命令详解
Sam在看相关PackageManager代码时,无意中发现Android 下提供一个pm命令,通常放在/system/bin/下.这个命令与Package有关,且非常实用.所以研究之.0. Usag ...
- javascript函数的基础功能
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- mongodb remove删除文档的用法
在看<mongoDB权威指南>中,在删除文档时,出现问题: 书中介绍:采用db.foo.remove()命令则可以删除foo集合中所有的文档,但是在执行该命令时,shell客户端却报错. ...
- go-vim配置
一.环境准备: 系统环境说明: [root@docker golang]# cat /etc/redhat-release CentOS Linux release (Core) [root@dock ...
- 自己写的sql server触发器练练--高手请您跳过吧
set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgo ALTER TRIGGER [insertReplyToic] ON [dbo].[bbsReplyTopic] ...
- HTML之学习笔记(八)表格
Html的表格使用table标签.table标签含有tr(table row)子标签,tr又含有th(table head)和td(table data)子标签这样的嵌套结构 代码演示 <tab ...
- Linux用户及用户组管理
Linux是个优秀的多用户多任务操作系统. 掌握Linux的用户/用户组管理是基本及必备技能之一. 简单做下总结. 无论采用图形界面的用户管理设置,还是终端的管理方式,最终目的都是对系统的用户/用户组 ...
- 防止自己的网站被别人frame引用造成钓鱼
自己负责的某一网站,最近被不法份子通过<frame>的方式引入,用户点击对方的域名后,看到的内容跟自己网站一模一样.但是右击查看源码就会发现其中的原理: <!DOCTYPE HTML ...