iOS8 UISearchViewController搜索功能讲解 分类: ios技术 2015-07-14 10:23 76人阅读 评论(0) 收藏
在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搜索功能讲解 分类: ios技术 2015-07-14 10:23 76人阅读 评论(0) 收藏的更多相关文章
- iOS越狱包 分类: ios相关 app相关 2015-06-10 10:53 152人阅读 评论(0) 收藏
编译完了的程序是xxx.app文件夹,我们需要制作成ipa安装包,方便安装 找一个不大于500*500的png图片(程序icon图标即可),改名为:iTunesArtwork,注意不能有后缀名. 建立 ...
- 8大排序算法图文讲解 分类: B10_计算机基础 2014-08-18 15:36 243人阅读 评论(0) 收藏
排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 常见的内部排序算法有:插入排序.希尔排序. ...
- JavaScript、Ajax与jQuery的关系 分类: C1_HTML/JS/JQUERY 2014-07-31 10:15 3388人阅读 评论(0) 收藏
简单总结: 1.JS是一门前端语言. 2.Ajax是一门技术,它提供了异步更新的机制,使用客户端与服务器间交换数据而非整个页面文档,实现页面的局部更新. 3.jQuery是一个框架,它对JS进行了封装 ...
- Nginx介绍 分类: Nginx 服务器搭建 2015-07-13 10:50 19人阅读 评论(0) 收藏
海量请求,高性能服务器. 对比Apache, Apache:稳定,开源,跨平台,重量级,不支持高度并发的web服务器. 由此,出现了Lighttpd与Nignx:轻量级,高性能. 发音:engine ...
- 全面解析sizeof(下) 分类: C/C++ StudyNotes 2015-06-15 10:45 263人阅读 评论(0) 收藏
以下代码使用平台是Windows7 64bits+VS2012. sizeof作用于基本数据类型,在特定的平台和特定的编译中,结果是确定的,如果使用sizeof计算构造类型:结构体.联合体和类的大小时 ...
- 全面解析sizeof(上) 分类: C/C++ StudyNotes 2015-06-15 10:18 188人阅读 评论(0) 收藏
以下代码使用平台是Windows7 64bits+VS2012. sizeof是C/C++中的一个操作符(operator),其作用就是返回一个对象或者类型所占的内存字节数,使用频繁,有必须对齐有个全 ...
- MS SQL 合并结果集并求和 分类: SQL Server 数据库 2015-02-13 10:59 92人阅读 评论(0) 收藏
业务情景:有这样一张表:其中Id列为表主键,Name为用户名,State为记录的状态值,Note为状态的说明,方便阅读. 需求描述:需要查询出这样的结果:某个人某种状态的记录数,如:张三,待审核记录数 ...
- 山东理工大学第七届ACM校赛-学区房问题 分类: 比赛 2015-06-26 10:23 89人阅读 评论(0) 收藏
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 铁牌狗在学区B有一套面积为S1平方米的房子,现在他为了让后代进化成金牌狗,决定在学区A购 ...
- 菜鸟学习-C语言函数参数传递详解-结构体与数组 分类: C/C++ Nginx 2015-07-14 10:24 89人阅读 评论(0) 收藏
C语言中结构体作为函数参数,有两种方式:传值和传址. 1.传值时结构体参数会被拷贝一份,在函数体内修改结构体参数成员的值实际上是修改调用参数的一个临时拷贝的成员的值,这不会影响到调用参数.在这种情况下 ...
随机推荐
- hadoop在子节点上没有datanode进程
经常会有这样的事情发生:在主节点上start-all.sh后,子节点有TaskTracker进程,而没有DataNode进程.环境:1NameNode 2DataNode三台机器,Hadoop为1 ...
- Extjs视频
Extjs视频http://www.youku.com/playlist_show/id_19343298.html ExtJs视频教程(关东升) 智捷关东升老师ExtJs视频教程AJAX框架-Ext ...
- AndroidGradle--瘦身apk(转发)
apk瘦身一般有两条线, 去除无用的代码,例如引用一个比较大的lib,只使用了其中很少的功能.其他无用的代码可以想办法去掉 去除无用的资源文件,可能是第三方lib中的,也有可能是开发中引入了无用的资源 ...
- 善用log日志
#-*- coding:utf-8 -*- import logging logger = logging.getLogger() #定义一个log日志对象 hdlr = logging.FileHa ...
- P2P应用中的NAT穿透问题
多年前曾经写过一个关于NAT钻洞的实验.现在发现那个做法在我现在的路由器上已经不管用了.经过一番搜索发现时过境迁,世界变化很快,新路由器已经是UPnP了.在这里重新理一下几种方法. 第一种,也是不太靠 ...
- Spring注解基本解读
在一个类中使用Spring对象,办法如下: 使用注解的形式注入 从ApplicationContext中获取. T t = new ApplicationContext.getBean("x ...
- 转 oracle 开发 第03章 sqlplus
目录 1.查看表结构 desc 2.编辑SQL语句 append.list.change.run 3.保存.检索并运行文件 save.get.start.edit.spool 4.格式化列 colum ...
- Fix a Tree
Fix a Tree A tree is an undirected connected graph without cycles. Let's consider a rooted undirecte ...
- 总结自己的Git常用命令
总结自己的Git常用命令 使用git也有一段时间了,把自己常用的命令用自己的描述记录起来,方便自己备忘也方便其他人参考. 目录: 最基本的命令: git clone 拷贝并跟踪远程的master分支. ...
- 最新百度地图支持Fragment(注意事项)(转)
原文: 最新百度地图支持Fragment(注意事项) 开篇:老的百度地图通常都要继承MapActivity,这样不利于代码的可扩展性,再加上Fragment的流行,老的百度地图已经远远不能满足的大 ...