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的一个提 ...
随机推荐
- gameUnity 网络游戏框架
常常在想,有没有好的方式,让开发变得简单,让团队合作更加容易. 于是,某一天 动手写一个 架构, 目前版本 暂定 0.1 版本.(unity5.0.0f4 版本以上) 我打算 开源出来 0.1有什么功 ...
- Json解析要点
解析Json时,昨天遇到了新的问题,之前都是解析的数组,不是数组的用类来做. 这是Json串; {"status":"00001","ver" ...
- Apache的安装
Apache的安装: 注:本例只截取需要注意的截图,其它默认则不显示. 1. 服务器信息可以按照默认配置,如果服务器的80端口没被其他服务器程序占据.可选“for All Users,on ...
- hdu_5418_Victor and World(状压DP+Floyd)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5418 题意:给你n个点,和一些边,找一条路径经过全部的点,并回到起点,问最小的花费是多少, 题解:m& ...
- [转] Spring Security(01)——初体验
[转自:http://haohaoxuexi.iteye.com/blog/2154299] 首先我们为Spring Security专门建立一个Spring的配置文件,该文件就专门用来作为Sprin ...
- ext3文件系统目录限制问题
昨晚排查了在KVM的build系统中的一个问题,跟踪到后面发现在一个目录下mkdir创建目录失败.我手动试了一下,提示如下:cannot create directory `/home/master/ ...
- 生成Token字符串
生成比较短的Token字符串 有的时候,我们需要生成一些Token作为标识:如认证后的标识符,资源的提取码等.一个比较常见的算法是生成一个GUID来作为Token,由于GUID的随机性和唯一性特点,作 ...
- zf-关于平台的用户名密码的设置
比如说安徽桐城的用户名密码在哪张表里设置 桐城市人民统一电子政务平台是http://localhost:8088/tc/ptzwfw.action 这个链接 在zwfw_tc 数据库的 PT_LOGI ...
- Not a million dollars ——a certain kind of ingenuity, discipline, and proactivity that most people seem to lack
原文:http://ryanwaggoner.com/2010/12/you-dont-really-want-a-million-dollars/a certain kind of ingenuit ...
- 一个基于jQuery的简单树形菜单
在工作中的项目使用的是一个前端基于 jQuery easyui 的一个系统,其中左侧的主菜单使用的是 easyui 中的 tree 组件,不是太熟悉,不过感觉不是太好用. 比如 easyui 中的 t ...