在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. javascript 总结学习一

    1 , javascript字符集:javascript采用的是Unicode字符集编码.为什么要采用这个编码呢?原因很简单,16位的Unicode编码可以表示地球人的任何书面语言.这是语言 国际化的 ...

  2. Git学习 -- 管理修改

    git关注的是修改,而不是文件 commit只会提交add到暂存区的修改 撤销修改 已修改但没有add到暂存区 git checkout -- <file>     #撤销工作区中的修改 ...

  3. C#无边框窗体移动 将事件绑定到想实现的控件上

    [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("use ...

  4. Oracle 使用sql创建表空间及用户

    create tablespace OrcalDBNamedb datafile 'C:\OracleDBDirc\OrcalDBNamedb.dbf' size 300m; 创建用户create u ...

  5. Android开发实现HttpClient工具类

    在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient来方便我们使用各种Http服务.你可以把HttpClient想 ...

  6. JAVA实现二进制,十六进制输出

    public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-gener ...

  7. HDU 1896 Stones(优先队列)

    还是优先队列 #include<iostream> #include<cstdio> #include<cstring> #include<queue> ...

  8. mr本地运行的几种模式

    MR程序的几种提交运行模式 本地模型运行 1/在windows的eclipse里面直接运行main方法,就会将job提交给本地执行器localjobrunner执行 ----输入输出数据可以放在本地路 ...

  9. 挂载了Cinder Volume的实例无法动态迁移排错

    现象:挂载了Cinder Volume的实例无法动态迁移 [root@node-5 nova]# tail -f compute.log 2016-01-13 16:36:12.870 18762 E ...

  10. Nested weights are bad for performance

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