在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搜索功能讲解的更多相关文章

  1. iOS8 UISearchViewController搜索功能讲解 分类: ios技术 2015-07-14 10:23 76人阅读 评论(0) 收藏

    在iOS8以前我们实现搜索功能需要用到UISearchbar和UISearchDisplayController, 在iOS8之后呢, UISearchController配合UITableView的 ...

  2. iOS--- UITableView + UISearchDisplayController - - - - -实现搜索功能

    iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 ...

  3. 011.Adding Search to an ASP.NET Core MVC app --【给程序添加搜索功能】

    Adding Search to an ASP.NET Core MVC app 给程序添加搜索功能 2017-3-7 7 分钟阅读时长 作者 本文内容 1.Adding Search by genr ...

  4. 电脑键盘上的F键有什么用 电脑F键功能讲解

    接触电脑这么多年了,F1到F12这几个键你真的会用吗?电脑键盘上的F键有什么用?你了解过吗?这里带来电脑F键功能讲解,一起来看看. F1:帮助 在程序里或者资源管理器界面,按F1会弹出帮助按钮. F2 ...

  5. GetX代码生成IDEA插件,超详细功能讲解(透过现象看本质)

    前言 本文章不是写getx框架的使用,而且其代码生成IDEA插件的功能讲解 我之前写过俩篇很长很长的getx文章 一篇入门使用:Flutter GetX使用---简洁的魅力! 一篇原理深度剖析:Flu ...

  6. Android搜索功能的案例,本地保存搜索历史记录......

    开发的APP有一个搜索功能,并且需要显示搜索的历史记录,我闲暇之余帮她开发了这个功能,现把该页面抽取成一个demo分享给大家. 实现效果如图所示:  本案例实现起来很简单,所以可以直接拿来嵌入项目中使 ...

  7. Yii 1开发日记 -- 搜索功能及Checkbox的实现

    用yii 1实现后台的搜索功能,效果如下图: 1.模型中: public function search() { $criteria = new CDbCriteria; //独立高级搜索 if(is ...

  8. SharePoint 2013 搜索功能,列表项目不能完全被索引

    描述 最近一个站点,需要开启搜索功能,然后创建内容源,开始爬网,发现列表里只有一部分被索引,很多项目没有被索引,甚是奇怪,如下图(其实列表里有80几条项目). 首先爬网账号是系统账号.服务器管理员,所 ...

  9. idea 光标变成粗体且当前文件搜索功能无法使用的问题

    今天安装了idea最新版,安装完成后发现光标变成了粗体,并且快捷键在使用时出现了问题,比如:ctrl+F搜索功能无法使用 经过反复修改配置也无法改善情况,后来一次重启看到下面小窗弹出有关vim的一个提 ...

随机推荐

  1. 转:loadrunner ---循环输出关联数组

    web_reg_save_param,将Ord参数值设定为ALL,则关联函数将自动把符合条件的关联值保存到参数数组里.在本例中,假设关联值返回三条记录,则LR分别将值保存到sor_1,sor_2,so ...

  2. The Accumulation of Capital

    The Accumulation of Capital ---- by *Adam Smith Capitals are increased by parsimony, and diminished ...

  3. awk 数组排序-- asort 与 asorti

    两者排序区别: asort 是对数组的值进行排序,并且会丢掉原先键值: asorti是对数组的下标进行排序. 数据文件: 12 34 78 90 23 45 1. awk是关联数组.for-in循环输 ...

  4. MySQL与MongoDB之SQL语法对比

  5. 关于window.location.href="delete_emp.do?id"+id;

    ?后面是参数 ?id 就是带参发送这个请求 参数就是id  后面的 +id 貌似 是值

  6. ignite学习笔记

    1.一个Ignite节点可以从命令行启动,可以用默认的配置也可以传递一个配置文件.可以启动很多很多的节点然后他们会自动地发现对方. 2.Ignite只需要一个ignite-core强依赖,通常你还需要 ...

  7. hibernate 使用sql 查询(setResultTransformer)

    使用方法举例如下: public List findByOid(Object oid) {  log.debug("finding all WatershedAnalyse instance ...

  8. css居中

    <html><head lang="en"> <meta charset="UTF-8"> <title>< ...

  9. JAVA字符串编码转换常用类

    无论是对程序的本地化还是国际化,都会涉及到字符编码的转换的问题.尤其在web应用中常常需要处理中文字符,这时就需要进行字符串的编码转换,将字符串编码转换为GBK或者GB2312.一.关键技术点:    ...

  10. .net core 14