在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. Universal-Image-Loader分析:

    Android-Universal-Image-Loader是一个开源的UI组件程序,该项目的目的是提供一个可重复使用的仪器为异步图像加载,缓存和显示.权限: <uses-permission ...

  2. 彻底搞明白find命令的-mtime参数的含义【转载】

    转自: 彻底搞明白find命令的-mtime参数的含义-goolen-ITPUB博客http://blog.itpub.net/23249684/viewspace-1156932/ 以前一直没有弄明 ...

  3. 设计模式之建造者模式(Builder)

    一个人活到70岁以上,都会经历这样的几个阶段:婴儿,少年,青年,中年,老年.并且每个人在各个阶段肯定是不一样的呀,我觉得可以说世界上不存在两个人在人生的这5个阶段的生活完全一样,但是活到70岁以上的人 ...

  4. Android 手机应用开发经验 之 通过Socket(TCP/IP)与PC通讯

    Android 是一个开源的手机操作系统平台,已经被非常多的开发者视作未来最有潜力的智能手机操作系统.而且,在很短的时间内就在Android Market上出现大量的第三方应用程序,供用户下载与使用, ...

  5. SQL Server2008数据库中删除用户,提示数据库主体在该数据库中拥有 架构,无法删除

    一个数据库,运行在SQL Server 2008下,数据库用户无法删除,在删除时提示“数据库主体在该数据库中拥有架构,无法删除”.原因很简单,就是由于此用户在数据库中拥有某些架构的所有权,将相关架构的 ...

  6. Win7+QTP10.0+IE9无法识别对象的解决方法

    在WIN7和IE9环境下使用QTP10,会出现识别不了web对象的情况,具体表现为:添加对象,先打开对象库,再打开IE,点击Add object to local 后,出现白色手指,将其拖拉到百度首页 ...

  7. PAT (Advanced Level) 1050. String Subtraction (20)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  8. 翻译的很好的一篇android mediaplayer

    MediaPlayer类可用于控制音频/视频文件或流的播放.关于如何使用这个类的方法还可以阅读VideoView类的文档. 1.状态图对播放音频/视频文件和流的控制是通过一个状态机来管理的.下图显示一 ...

  9. Jobject 使用

    obj2 = JObject.Parse(result.Html); JArray _Jarr = _obj["data"]["siteList"].Value ...

  10. CSS的三种手段让元素脱离标准本文档流——浮动、绝对定位、固定定位

    1.浮动 浮动是CSS中用到的最多的一个选项,他有三个性质.关于浮动我们要强调一点,永远不是一个东西单独浮动,浮动都是一起浮动,要浮动,大家都浮动. 1.1 浮动元素脱离标准文档流 1.1.1 大概描 ...