In iOS 8.0 or later, we can use Uisearchcontroller to easily add a search box to UITableView. In previous versions, we had to use the Uisearchbar + uisearchdisplaycontroller combination.
To add the Uisearchcontroller property:
@property(strongnonatomic) UISearchController *searchController;@property(strongnonatomicNSMutableArray// 所有城市@property(strongnonatomicNSMutableArray// 根据searchController搜索的城市
Uisearchcontroller initialization
Initialize the Uisearchcontroller in Viewdidload:
Self. Searchcontroller= [[Uisearchcontroller alloc] Initwithsearchresultscontroller:nil];Self. Searchcontroller. Searchresultsupdater= Self;Self. Searchcontroller. Dimsbackgroundduringpresentation= False;[Self. Searchcontroller. SearchbarSizeToFit];Self. Searchcontroller. Searchbar. BackgroundColor= Uicolorfromhex (0XDCDCDC);Self. TableView. Tableheaderview= Self. Searchcontroller. Searchbar;
Uisearchresultsupdating protocol
Use Uisearchcontroller to inherit the Uisearchresultsupdating protocol and implement the Uisearchresultsupdating method.
#pragma mark-searchcontroller Delegate- (void) Updatesearchresultsforsearchcontroller: (Uisearchcontroller *) Searchcontroller {[ Self. FilteredcitiesRemoveallobjects]; Nspredicate *searchpredicate = [Nspredicate predicatewithformat:@"Self contains[c]%@", Self. Searchcontroller. Searchbar. Text]; Self. Filteredcities= [[ Self. AllcitiesFilteredarrayusingpredicate:searchpredicate] mutablecopy];Dispatch_async(Dispatch_get_main_queue (), ^{[ Self. TableViewReloaddata]; });}
The method is called when the content in the Uisearchcontroller searchbar changes. In which we can use nspredicate to set conditions for search filtering.
Update UITableView
After introducing Uisearchcontroller, the contents of UITableView should be changed accordingly: that is, the content of the cell to be presented is allcities, or filteredcities.
This can be judged by the active property of Uisearchcontroller, that is, whether the input box is in the active state.
Many of the methods related to UITableView are judged according to active:
- (Nsinteger) Numberofsectionsintableview: (UITableView*) TableView {if(! Self. Searchcontroller. Active) {return Self. Citykeys. Count; }Else{return 1; }}- (Nsinteger) TableView: (UITableView*) TableView numberofrowsinsection: (Nsinteger) Section {if(! Self. Searchcontroller. Active) {NSString*key = Self. Citykeys[section];Nsarray*citysection = Self. Citydict[Key];returnCitysection. Count; }Else{return Self. Filteredcities. Count; }}- (UITableViewCell*) TableView: (UITableView*) TableView Cellforrowatindexpath: (Nsindexpath*) Indexpath {Static NSString*cellidentifier = @"Cell";UITableViewCell*cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];if(Cell = =Nil) {cell = [[UITableViewCellAlloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:cellidentifier]; Cell. Selectionstyle= Uitableviewcellselectionstylenone; }//Based on Uisearchcontroller's active property to determine the contents of the cell if(! Self. Searchcontroller. Active) {NSString*key = Self. Citykeys[Indexpath. section]; Cell. Textlabel. Text= [ Self. Citydict[Key] Objectatindex:indexpath. Row]; }Else{cell. Textlabel. Text= Self. Filteredcities[Indexpath. Row]; }returnCell;}
Removal of Uisearchcontroller
In Viewwilldisappear to remove the Uisearchcontroller, or switch to the next view, the search box will still have a brief existence.
- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; if (self.searchController.active) { self.searchController.activeNO; [self.searchController.searchBar removeFromSuperview]; }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
IOS---The use of the search box Uisearchcontroller (iOS8.0 later replaces Uisearchbar + Uisearchdisplaycontroller combination)