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的一个提 ...
随机推荐
- 自定义开关ToggleButton
package com.example.test;import android.os.Bundle;import android.app.Activity;import android.view.Me ...
- gcc编译错误表
conversion from %s to %s not supported by iconv”iconv 不支持从 %s 到 %s 的转换” iconv_open”iconv_open” no ic ...
- useradd adduer 的区别
区别 1). 使用useradd时,如果后面不添加任何参数选项,例如:#sudo useradd test创建出来的用户将是默认“三无”用户:一无Home Directory,二无密码,三无系统She ...
- sites
search http://213.240.44.13/ http://1.179.252.95/
- 服务器遭受 ssh 攻击
查看auth.log日志,差点吓一跳,好多攻击记录. vim /var/log/auth.log 才两天的功夫,900多万条记录, 一些解决应对的办法: 43down voteaccepted It ...
- automaticallyAdjustsScrollViewInsets 标签栏不正常显示
想做的效果如下: 结果那个首页.手办模型神马的就是不显示啊... 这个标签栏是用scrollView做的,解决办法: viewController.automaticallyAdjustsScroll ...
- 【转】SSL/TLS/WTLS协议原理
1 SSL(Secure Socket Layer)是netscape公司设计的主要用于web的安全传输协议.这种协议在WEB上获得了广泛的应用.2 IETF(www.ietf.org )将SSL作了 ...
- 注册表检测office版本
#region 查询注册表,判断本机是否安装Office2003,2007和WPS public int ExistsRegedit() { int ifused = 0; RegistryKey r ...
- 解析json数组
解析json数组 JSONArray jsonArray = new JSONArray(markingStr); int iSize = jsonArray.length(); for (int i ...
- Redis缓存服务搭建及实现数据读写--转载
来自 http://www.cnblogs.com/lc-chenlong/p/3218157.html 1. 下载安装Redis 下载地址:https://github.com/MSOpenTec ...