iOS8 UISearchViewController搜索功能講解

在iOS8之前咱們實現搜索功能須要用到UISearchbar和UISearchDisplayController, 在iOS8以後呢, UISearchController配合UITableView的使用相比之下簡單不少,  須要簽定兩個代理協議UISearchControllerDelegate, UISearchResultsUpdating.還有一個很重要的屬性self.searchVC.active,,返回的BOOL若是爲yes,UITableView的數據源應該爲搜索後的數組即resultArray, 不然爲原始數組即self.dataArray-------須要在UITableView的代理方法裏作判斷.  運行效果圖以下:git

     

 

具體代碼以下:github

 

 

.h文件數組

 

//  ViewController.hatom

//  SearchForChinese代理

//  Created by Dong on 15/5/14.orm

//  Copyright (c) 2015年 xindong. All rights reserved.排序

#import <UIKit/UIKit.h>it

#import "ChinesePinyin/ChineseSorting.h"io

@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource,UISearchControllerDelegate, UISearchResultsUpdating>table

 

@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

相關文章
相關標籤/搜索