在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) 收藏的更多相关文章

  1. iOS越狱包 分类: ios相关 app相关 2015-06-10 10:53 152人阅读 评论(0) 收藏

    编译完了的程序是xxx.app文件夹,我们需要制作成ipa安装包,方便安装 找一个不大于500*500的png图片(程序icon图标即可),改名为:iTunesArtwork,注意不能有后缀名. 建立 ...

  2. 8大排序算法图文讲解 分类: B10_计算机基础 2014-08-18 15:36 243人阅读 评论(0) 收藏

    排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 常见的内部排序算法有:插入排序.希尔排序. ...

  3. JavaScript、Ajax与jQuery的关系 分类: C1_HTML/JS/JQUERY 2014-07-31 10:15 3388人阅读 评论(0) 收藏

    简单总结: 1.JS是一门前端语言. 2.Ajax是一门技术,它提供了异步更新的机制,使用客户端与服务器间交换数据而非整个页面文档,实现页面的局部更新. 3.jQuery是一个框架,它对JS进行了封装 ...

  4. Nginx介绍 分类: Nginx 服务器搭建 2015-07-13 10:50 19人阅读 评论(0) 收藏

    海量请求,高性能服务器. 对比Apache, Apache:稳定,开源,跨平台,重量级,不支持高度并发的web服务器. 由此,出现了Lighttpd与Nignx:轻量级,高性能. 发音:engine ...

  5. 全面解析sizeof(下) 分类: C/C++ StudyNotes 2015-06-15 10:45 263人阅读 评论(0) 收藏

    以下代码使用平台是Windows7 64bits+VS2012. sizeof作用于基本数据类型,在特定的平台和特定的编译中,结果是确定的,如果使用sizeof计算构造类型:结构体.联合体和类的大小时 ...

  6. 全面解析sizeof(上) 分类: C/C++ StudyNotes 2015-06-15 10:18 188人阅读 评论(0) 收藏

    以下代码使用平台是Windows7 64bits+VS2012. sizeof是C/C++中的一个操作符(operator),其作用就是返回一个对象或者类型所占的内存字节数,使用频繁,有必须对齐有个全 ...

  7. MS SQL 合并结果集并求和 分类: SQL Server 数据库 2015-02-13 10:59 92人阅读 评论(0) 收藏

    业务情景:有这样一张表:其中Id列为表主键,Name为用户名,State为记录的状态值,Note为状态的说明,方便阅读. 需求描述:需要查询出这样的结果:某个人某种状态的记录数,如:张三,待审核记录数 ...

  8. 山东理工大学第七届ACM校赛-学区房问题 分类: 比赛 2015-06-26 10:23 89人阅读 评论(0) 收藏

    Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 铁牌狗在学区B有一套面积为S1平方米的房子,现在他为了让后代进化成金牌狗,决定在学区A购 ...

  9. 菜鸟学习-C语言函数参数传递详解-结构体与数组 分类: C/C++ Nginx 2015-07-14 10:24 89人阅读 评论(0) 收藏

    C语言中结构体作为函数参数,有两种方式:传值和传址. 1.传值时结构体参数会被拷贝一份,在函数体内修改结构体参数成员的值实际上是修改调用参数的一个临时拷贝的成员的值,这不会影响到调用参数.在这种情况下 ...

随机推荐

  1. Android 系统编译

    最近研究了下Android 的编译系统,下面结合编译我们自己的产品 mobot 来对整个编译系统进行必要的介绍,方便大家今 后对默认编译的修改. 先列出几个觉得重要的Make 文件: build/bu ...

  2. 一个很好的通用 excel 导出工具类

    此类用主要 jxl +注解+流 实现扩展性很强,jxl性能会比poi好一点,值得我们学习. package oa.common.utils; import java.io.OutputStream; ...

  3. spring MVC之返回JSON数据(Spring3.0 MVC)

    方式一:使用ModelAndView的contentType是"application/json" 方式二:返回String的            contentType是&qu ...

  4. 【摘自网络】dll库和lib库有什么区别

    简单地讲:第一:.DLL是动态链接库,而.LIB是静态链接库dll是个编译好的程序,调用时可以直接调用其中的函数,不参加工程的编译. 而lib应该说是一个程序集, 只是把一些相应的函数总结在一起, 如 ...

  5. shell执行php文件传递参数

    php -f index.php hello test 2314 shell命令执行php文件不像http那样通过GET方式传参 同样php文件获取的时候也不能用$_GET方法了 而是通过$argv[ ...

  6. 解读QML之二

    QML文档 QML文档是用QML语法组成的字符串.一个文档定义了一个QML对象类型.文档以”.qml”最为后缀,可以保存在本地和网络上,可以使用代码生成.一 个在文档中定义的对象类型的实例,也可以使用 ...

  7. SQL清除所有数据库日志脚本

    --SQL清除所有数据库日志脚本 declare @CurrentDataBaseName nvarchar(100) declare @CurrentDataBaseID nvarchar(100) ...

  8. 转 ogg组件介绍

    应用场景:数据分发   ogg的组件: (1) OGG 程序和工具说明 convchk   转换ogg版本的信息 ,该程序可以将checkpoint files 转换成新版本: convprm :OG ...

  9. 转 :Oracle分区表 (Partition Table) 的创建及管理

    三.删除分区 You can drop partitions from range, list, or composite range-list partitioned tables. ALTER T ...

  10. android 画图之setXfermode .

    setXfermode 设置两张图片相交时的模式 我们知道 在正常的情况下,在已有的图像上绘图将会在其上面添加一层新的形状. 如果新的Paint是完全不透明的,那么它将完全遮挡住下面的Paint: 而 ...