iOS8 UISearchViewController搜索功能讲解
在iOS8以前我们实现搜索功能需要用到UISearchbar和UISearchDisplayController, 在iOS8之后呢, UISearchController配合UITableView的使用相比之下简单很多, 需要签订两个代理协议UISearchControllerDelegate, UISearchResultsUpdating.还有一个很重要的属性self.searchVC.active,,返回的BOOL如果为yes,UITableView的数据源应该为搜索后的数组即resultArray, 否则为原始数组即self.dataArray-------需要在UITableView的代理方法里做判断. 运行效果图如下:
具体代码如下:
.h文件
// ViewController.h
// SearchForChinese
// Created by Dong on 15/5/14.
// Copyright (c) 2015年 xindong. All rights reserved.
#import <UIKit/UIKit.h>
#import "ChinesePinyin/ChineseSorting.h"
@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource,UISearchControllerDelegate, UISearchResultsUpdating>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UISearchController *searchVC;
// 存放排好序的数据(汉字)
@property (nonatomic, strong) NSMutableDictionary *sectionDictionary;
// 存放汉字拼音大写首字母
@property (nonatomic, strong) NSArray *keyArray;
// 存放汉字(地名)
@property (nonatomic, strong) NSMutableArray *dataArray;
@end
.m文件
// ViewController.m
// SearchForChinese
// Created by Dong on 15/5/14.
// Copyright (c) 2015年 xindong. All rights reserved.
#import "ViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
label.backgroundColor = [UIColor greenColor];
label.text = @"\n搜搜";
label.numberOfLines = 0;
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT)style:UITableViewStyleGrouped];
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
// UISearchController初始化
self.searchVC = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchVC.searchResultsUpdater = self;
self.searchVC.delegate = self;
self.searchVC.searchBar.frame = CGRectMake(0, 100, WIDTH, 44);
self.searchVC.searchBar.barTintColor = [UIColor yellowColor];
self.tableView.tableHeaderView = self.searchVC.searchBar;
// 设置为NO,可以点击搜索出来的内容
self.searchVC.dimsBackgroundDuringPresentation = NO;
// 原始数据
self.dataArray = [NSMutableArray arrayWithObjects:@"大兴", @"丰台", @"海淀", @"朝阳", @"东城", @"崇文", @"西城", @"石景山",@"通州", @"密云", @"迪拜", @"华仔", @"三胖子", @"大连", nil];
[self customSortingOfChinese:self.dataArray];
}
/*
**
**********************调用排序方法 (本人自己封装的方法, 下载地址:https://github.com/Tbwas/ChineseCharacterSorting)
*
**/
- (void)customSortingOfChinese:(NSMutableArray *)array
{
// 获取汉字拼音首字母
self.keyArray = [[ChineseSorting sharedInstance] firstCharcterSortingOfChinese:array];
// 将汉字排序
self.sectionDictionary = [NSMutableDictionary dictionary];
self.sectionDictionary = [[ChineseSorting sharedInstance] chineseCharacterSorting:arrayKeyArray:self.keyArray];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.keyArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 取每个section对应的数组
NSArray *arr = [self.sectionDictionary objectForKey:self.keyArray[section]];
return arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *str = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:str];
}
NSArray *arr = [self.sectionDictionary objectForKey:self.keyArray[indexPath.section]];
cell.textLabel.text = arr[indexPath.row];
return cell;
}
// section的标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return self.keyArray[section];
}
// 搜索界面将要出现
- (void)willPresentSearchController:(UISearchController *)searchController
{
NSLog(@"将要 开始 搜索时触发的方法");
}
// 搜索界面将要消失
-(void)willDismissSearchController:(UISearchController *)searchController
{
NSLog(@"将要 取消 搜索时触发的方法");
}
-(void)didDismissSearchController:(UISearchController *)searchController
{
[self customSortingOfChinese:self.dataArray];
[self.tableView reloadData];
}
#pragma mark -- 搜索方法
// 搜索时触发的方法
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchStr = [self.searchVC.searchBar text];
// 谓词
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@", searchStr];
// 过滤数据
NSMutableArray *resultDataArray = [NSMutableArray arrayWithArray:[self.dataArray filteredArrayUsingPredicate:predicate]];
// 调用地名排序的方法
[self customSortingOfChinese:resultDataArray];
// 刷新列表
[self.tableView reloadData];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertView *arlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"表点啦"delegate:nil cancelButtonTitle:nil otherButtonTitles:@"听话", nil];
[arlertView show];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
iOS8 UISearchViewController搜索功能讲解的更多相关文章
- iOS8 UISearchViewController搜索功能讲解 分类: ios技术 2015-07-14 10:23 76人阅读 评论(0) 收藏
在iOS8以前我们实现搜索功能需要用到UISearchbar和UISearchDisplayController, 在iOS8之后呢, UISearchController配合UITableView的 ...
- iOS--- UITableView + UISearchDisplayController - - - - -实现搜索功能
iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 ...
- 011.Adding Search to an ASP.NET Core MVC app --【给程序添加搜索功能】
Adding Search to an ASP.NET Core MVC app 给程序添加搜索功能 2017-3-7 7 分钟阅读时长 作者 本文内容 1.Adding Search by genr ...
- 电脑键盘上的F键有什么用 电脑F键功能讲解
接触电脑这么多年了,F1到F12这几个键你真的会用吗?电脑键盘上的F键有什么用?你了解过吗?这里带来电脑F键功能讲解,一起来看看. F1:帮助 在程序里或者资源管理器界面,按F1会弹出帮助按钮. F2 ...
- GetX代码生成IDEA插件,超详细功能讲解(透过现象看本质)
前言 本文章不是写getx框架的使用,而且其代码生成IDEA插件的功能讲解 我之前写过俩篇很长很长的getx文章 一篇入门使用:Flutter GetX使用---简洁的魅力! 一篇原理深度剖析:Flu ...
- Android搜索功能的案例,本地保存搜索历史记录......
开发的APP有一个搜索功能,并且需要显示搜索的历史记录,我闲暇之余帮她开发了这个功能,现把该页面抽取成一个demo分享给大家. 实现效果如图所示: 本案例实现起来很简单,所以可以直接拿来嵌入项目中使 ...
- Yii 1开发日记 -- 搜索功能及Checkbox的实现
用yii 1实现后台的搜索功能,效果如下图: 1.模型中: public function search() { $criteria = new CDbCriteria; //独立高级搜索 if(is ...
- SharePoint 2013 搜索功能,列表项目不能完全被索引
描述 最近一个站点,需要开启搜索功能,然后创建内容源,开始爬网,发现列表里只有一部分被索引,很多项目没有被索引,甚是奇怪,如下图(其实列表里有80几条项目). 首先爬网账号是系统账号.服务器管理员,所 ...
- idea 光标变成粗体且当前文件搜索功能无法使用的问题
今天安装了idea最新版,安装完成后发现光标变成了粗体,并且快捷键在使用时出现了问题,比如:ctrl+F搜索功能无法使用 经过反复修改配置也无法改善情况,后来一次重启看到下面小窗弹出有关vim的一个提 ...
随机推荐
- nginx之fastcgi
fastcgi的应用程序就是一个while循环在,不停的accept,如果收到相应的服务请求则负责服务并将结果返回. 在fastcgi的进程环境中,标准输入与标准输出已经被重定向到了监听的socket ...
- Theos tweak MSHookFunction
#import "substrate.h" static FILE * (*s_orig_fopen) ( const char * filename, const char * ...
- Git 分支 - 分支的衍合
分支的衍合 把一个分支中的修改整合到另一个分支的办法有两种:merge 和 rebase(译注:rebase 的翻译暂定为“衍合”,大家知道就可以了.).在本章我们会学习什么是衍合,如何使用衍合,为什 ...
- PD生成oracle表名带引号解决方案
使用PowerDesigner生成数据库建表SQL脚本时,尤其是Oracle数据库时,表名一般会带引号.其实加引号是PL/SQL的规范,数据库会 严格按照“”中的名称建表,如果没有“”,会按照ORAC ...
- 提升html5的性能体验系列之一避免切页白屏
窗体切换白屏的现实问题 HTML5的性能比原生差很多,比如切页时白屏.列表滚动不流畅.下拉刷新和上拉翻页卡顿.在低端Android手机上,很多原生App常用的功能和体验效果都很难使用HTML5技术模拟 ...
- linux仅修改文件夹权限;linux 分别批量修改文件和文件夹权限
比如我想把/var/www/html下的文件全部改成664,文件夹改成775,怎么做呢 方法一: 先把所有文件及文件夹改成664,然后把所有文件夹改成775 root@iZ25bq9kj7yZ:/ c ...
- android:分享 一个很强大的LOG开关---Log.isLoggable
标签:android分享 一个很强大的log开 1.API亮点: 此API可以实现不更换APK,在出问题的手机上就直接能抓到有效log,能提升不少工作效率. 2.API介绍 最近在解决短信问题时,看到 ...
- CentOS 安装Chrome
yum install http://people.centos.org/hughesjr/chromium/6/x86_64/RPMS/chromium-31.0.1650.63-2.el6.x86 ...
- C# 日期时间
//获取日期+时间 DateTime.Now.ToString(); // 2008-9-4 20:02:10 DateTime.Now.ToLocalTime().ToString(); // 20 ...
- HDU 2087 剪花布条(KMP基础应用)
KMP基础,注意输入 #include<cstdio> #include<cstring> #include<iostream> using namespace s ...