IOS---The use of the search box Uisearchcontroller (iOS8.0 later replaces Uisearchbar + Uisearchdisplaycontroller combination)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.