在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. css 7.30

    1.外提到内联元素,我们会想到有个display的属性是display:inline;这个属性能够修复著名的IE双倍浮动边界(float时margin)问题 2.一般来说,可以为所有块级元素应用 te ...

  2. [原]界面上有几个球队名字的列表,将鼠标放到球队名字上就变为红色背景,其他球队背景颜色为白色,点击一个球队的时候就将点击的球队变为fontSize=30字体(fontSize=‘’回到默认)。

    window.onload = function () {             var uls = document.getElementById('ui').getElementsByTagNa ...

  3. Dev之ChartControl控件(二)— 绘制多重坐标图形

    有时针对一个ChartControl控件可能要设置多个Y轴,进行显示: 以下举个例子:如在一个Chart中显示多个指标项如图: 首先,读取数据,并对左边的Y轴最大和最小值进行设定 IndexSerie ...

  4. 一个简单的基于HTTP协议的屏幕共享应用

    HTTP协议可以能是应用层协议里使用最广泛并且用途最多样的一个了.我们一般使用HTTP协议来浏览网页,但是HTTP协议还用来做很多其它用途.对开发人员来讲很常见的一种就是用HTTP协议作为各种版本控制 ...

  5. ASP.NET Cache 类

    在查找资料的过程中.原来园子里面已经有过分析了.nopCommerce架构分析系列(二)数据Cache. 接下来是一些学习补充. 1.Nop中没有System.Web.Caching.Cache的实现 ...

  6. 【转】获取/设置IFRAME内对象元素的几种JS方法

    1.IE专用(通过frames索引形象定位): document.frames[i].document.getElementById('元素的ID'); 2.IE专用(通过IFRAME名称形象定位): ...

  7. They Are Everywhere

    They Are Everywhere Sergei B., the young coach of Pokemons, has found the big house which consists o ...

  8. linux下的文本处理命令sed&awk&grep

    Sedsed 是个精简的.非交互式的编辑器.他能执行和编辑vi和emacs相同的编辑任务.sed编辑器不提供交互使用方式:只能在命令行输入编辑命令.指定文件名,然后在屏幕上察看输出.sed编辑器没有破 ...

  9. hrbustoj 2033 A Funny Game(对称博弈)

    对称博弈,注释在代码里 #include<iostream> #include<cstdio> using namespace std; ///这个地方其实是博弈原理里面的对称 ...

  10. Nested weights are bad for performance

    警告信息“Nested weights are bad for performance”的消除方法 原因分析:在布局进行嵌套使用时,父布局与子布局都使用了android:layout_weight,但 ...