在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 = @" 搜搜";
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