In the iOS8 before we implement the search function need to use Uisearchbar and Uisearchdisplaycontroller, after IOS8, Uisearchcontroller with UITableView the use of the comparison is much simpler, Need to sign two agent agreement Uisearchcontrollerdelegate, uisearchresultsupdating. There is also a very important property self.searchVC.active, which returns the bool if yes, The UITableView data source should be resultarray after the searched array, otherwise the original array is self.dataarray-------need to be judged in the proxy method of UITableView. Run as follows:
The specific code is as follows:
. h file
ViewController.h
Searchforchinese
Created by 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;
Storing sequenced data (kanji)
@property (nonatomic, strong) Nsmutabledictionary *sectiondictionary;
Store Chinese Pinyin Capital initials
@property (nonatomic, strong) Nsarray *keyarray;
Storing Chinese characters (place names)
@property (nonatomic, strong) Nsmutablearray *dataarray;
@end
. m file
Viewcontroller.m
Searchforchinese
Created by 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];
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 Search";
Label.numberoflines = 0;
Label.textalignment = Nstextalignmentcenter;
[Self.view Addsubview:label];
Self.tableview = [[UITableView alloc] Initwithframe:cgrectmake (0, WIDTH, HEIGHT) style:uitableviewstylegrouped];
Self.tableView.backgroundColor = [Uicolor Clearcolor];
Self.tableView.delegate = self;
Self.tableView.dataSource = self;
[Self.view AddSubview:self.tableView];
Uisearchcontroller initialization
SELF.SEARCHVC = [[Uisearchcontroller alloc] initwithsearchresultscontroller:nil];
Self.searchVC.searchResultsUpdater = self;
Self.searchVC.delegate = self;
Self.searchVC.searchBar.frame = CGRectMake (0, +, WIDTH, 44);
Self.searchVC.searchBar.barTintColor = [Uicolor Yellowcolor];
Self.tableView.tableHeaderView = Self.searchVC.searchBar;
Set to No, you can click on the search-out content
Self.searchVC.dimsBackgroundDuringPresentation = NO;
Raw data
Self.dataarray = [Nsmutablearray arraywithobjects:@ "Daxing", @ "Fengtai", @ "Haidian", @ "Chaoyang", @ "Dongcheng", @ "Chongwen", @ "Xicheng", @ "Shijingshan", @ "Tongzhou", @ "Miyun", @ " Dubai ", @" Hua Zi ", @" three fats ", @" Dalian ", Nil];
[Self customSortingOfChinese:self.dataArray];
}
/*
**
Call sorting method (myself encapsulated method,: https://github.com/Tbwas/ChineseCharacterSorting)
*
**/
-(void) Customsortingofchinese: (Nsmutablearray *) array
{
Get the first letter of Chinese pinyin
Self.keyarray = [[chinesesorting sharedinstance] firstcharctersortingofchinese:array];
Sort the Kanji
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
{
Take the corresponding array for each 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;
}
Title of section
-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) Section
{
return self.keyarray[section];
}
The search interface will appear
-(void) Willpresentsearchcontroller: (Uisearchcontroller *) Searchcontroller
{
NSLog ("The method to trigger when the search is about to begin");
}
The search interface is going to disappear
-(void) Willdismisssearchcontroller: (Uisearchcontroller *) Searchcontroller
{
NSLog (@ "The method that fires when the search is about to be canceled");
}
-(void) Diddismisssearchcontroller: (Uisearchcontroller *) Searchcontroller
{
[Self customSortingOfChinese:self.dataArray];
[Self.tableview Reloaddata];
}
#pragma mark--search method
Methods that are triggered when searching
-(void) Updatesearchresultsforsearchcontroller: (Uisearchcontroller *) Searchcontroller
{
NSString *SEARCHSTR = [Self.searchVC.searchBar text];
Predicate
Nspredicate *predicate = [Nspredicate predicatewithformat:@ "Self CONTAINS%@", Searchstr];
Filtering data
Nsmutablearray *resultdataarray = [Nsmutablearray arraywitharray:[self.dataarray filteredarrayusingpredicate: predicate]];
Methods for calling place-name sorting
[Self customsortingofchinese:resultdataarray];
Refresh List
[Self.tableview Reloaddata];
}
-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath
{
Uialertview *arlertview = [[Uialertview alloc] initwithtitle:@ "hint" message:@ "table point" Delegate:nil cancelbuttontitle:nil otherbuttontitles:@ "obedient", nil];
[Arlertview show];
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
@end
IOS8 Uisearchviewcontroller Search Function explained