The importance of the search bar we will not say, The Wolf factory is relying on search, now more and more like a wolf without moral integrity, UC Browser search bar is now the default home of God horse search, now whether it is social, online education, etc. will have a search bar implementation, but the effect is different from each other. The Search bar in iOS is relatively simple to implement, there are a lot of references on the Internet, but not many, many are the implementation before iOS 8.0, iOS 8.0 implementation seems to see very little, see some of the foreigner's Code, The use of a bit of uisearchcontroller feeling is very good.
Uisearchbar and Uidisplaycontroller
is the most common on the Internet is also the simplest, also have the use of Searh Bar Search Display Controller control, this article on the simple use of search bar and UITableView implementation of the searching demo, the top is the search bar, Before that was TableView:
In order to implement the search needs to declare the delegate uisearchbardelegate,uisearchdisplaydelegate, where the search is mainly used uisearchdisplaydelegate, Specific code implementation process:
Declaring fields:
@property (strong,nonatomic) nsmutablearray *datalist; @property (strong,nonatomic) Nsmutablearray * SearchList;
Initialize data:
Self.datalist=[nsmutablearray arraywithcapacity:100]; for (Nsinteger i=0; i<100; i++) { [self.datalist addobject:[nsstring stringwithformat:@ '%ld-flyelephant ', (long ) i]]; }
Set Area:
Set area-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{ return 1;}
Set the number of rows in a range (emphasis), which is the use of a delegate after you need to determine whether it is necessary to use the search after the view:
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{ if (TableView = = Self.searchDisplayController.searchResultsTableView) { return [self.searchlist count]; } else{ return [self.datalist count]; }}
The same return cell also has two cases, one is the initialization data, and the other is the filtered Data view:
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{ static NSString *[email protected] "Cellflag"; UITableViewCell *cell=[tableview Dequeuereusablecellwithidentifier:flag]; if (cell==nil) { Cell=[[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseIdentifier:flag]; } if (tableview==self.searchdisplaycontroller.searchresultstableview) { [Cell.textlabel setText: Self.searchlist[indexpath.row]]; } else{ [Cell.textlabel settext:self.datalist[indexpath.row]; } return cell;}
Uisearchbardelegate Start and End events in Germany :
-(BOOL) searchbarshouldbeginediting: (Uisearchbar *) searchbar{ NSLog (@ "search begin"); return YES;} -(BOOL) searchbarshouldendediting: (Uisearchbar *) searchbar{ NSLog (@ "search End"); return YES;}
Filter data when searching:
-(BOOL) Searchdisplaycontroller: (Uisearchdisplaycontroller *) controller shouldreloadtableforsearchstring: ( NSString *) searchstring{ //predicate contains syntax, previous article introduced http://www.cnblogs.com/xiaofeixiang/ nspredicate *preicate = [ Nspredicate predicatewithformat:@ "Self contains[c]%@", searchstring]; if (self.searchlist!= nil) { [self.searchlist removeallobjects]; } Filter data self.searchlist= [Nsmutablearray arraywitharray:[_datalist filteredarrayusingpredicate:preicate]]; Refresh the form return YES;}
The final effect is as follows:
Uisearchcontroller Implementation Search
uiseachbar implementation of the above effect is not a problem, there are many similar implementations on the Internet, It's just a warning, as follows: ' Searchdisplaycontroller ' is deprecated:first deprecated in IOS 8.0, so obviously a warning can't be ignored , found in StackOverflow Uisearchdisplaycontroller was deprecated in IOS8. 0, and recommended To use Uisearchcontroller instead, which means IOS 8.0 not recommend Uisearchdisplaycontroller, that is, not recommended uisearchdisplaydelegate, but can be implemented through Uisearchcontroller uisearchresultsupdating This delegate realizes the above effect;
You need to declare uisearchresultsupdating in the view:
@interface Viewcontroller:uitableviewcontroller<uitableviewdelegate,uitableviewdatasource, Uisearchbardelegate,uisearchresultsupdating> @end
Attribute declaration:
@property (nonatomic, strong) Uisearchcontroller *searchcontroller;
You need to initialize the Uisearchcontroller yourself:
_searchcontroller = [[Uisearchcontroller alloc] initwithsearchresultscontroller:nil]; _searchcontroller.searchresultsupdater = self; _searchcontroller.dimsbackgroundduringpresentation = NO; _searchcontroller.hidesnavigationbarduringpresentation = NO; _searchcontroller.searchbar.frame = CGRectMake (self.searchcontroller.searchbar.frame.origin.x, SELF.SEARCHCONTROLLER.SEARCHBAR.FRAME.ORIGIN.Y, Self.searchController.searchBar.frame.size.width, 44.0); Self.tableView.tableHeaderView = Self.searchController.searchBar;
Before the TableView by judging the search time, but now directly using self.searchController.active to judge, that is, the active property of Uisearchcontroller:
//sets the number of rows in the area-(Nsinteger) TableView: (UITableView *) TableView Numberofrowsinsection: (Nsinteger) section{if (self.searchController.active) {return [SELF.S Earchlist Count]; }else{return [self.datalist Count]; }//return cell contents-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{s Tatic nsstring *[email protected] "Cellflag"; UITableViewCell *cell=[tableview Dequeuereusablecellwithidentifier:flag]; if (cell==nil) {Cell=[[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseIdentifier:flag]; } if (self.searchController.active) {[Cell.textlabel settext:self.searchlist[indexpath.row]]; } else{[Cell.textlabel Settext:self.datalist[indexpath.row]]; } return cell;
The method used at the time of the call has also changed, using Updatesearchresultsforsearchcontroller to filter the results:
-(void) Updatesearchresultsforsearchcontroller: (Uisearchcontroller *) Searchcontroller { NSString *searchString = [Self.searchController.searchBar text]; Nspredicate *preicate = [Nspredicate predicatewithformat:@ "self contains[c]%@", searchstring]; if (self.searchlist!= nil) { [self.searchlist removeallobjects]; } Filter data self.searchlist= [Nsmutablearray arraywitharray:[_datalist filteredarrayusingpredicate:preicate]]; Refresh Table [Self.tableview reloaddata];}
Effect Demo:
However, the results of the final realization of the effect is basically the same, the same, this article will inevitably be omitted, if there is improper, please correct me ~
Resources:
Https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UISearchController/index.html #//apple_ref/occ/instp/uisearchcontroller/searchbar
iOS Development-Search bar Uisearchbar and Uisearchcontroller