Uisearchdisplaycontroller for Search in iOS, the importance of the search bar we will not say, The Wolf factory is relying on the search, now more and more like a wolf without moral integrity, UC Browser search bar now default home of God horse search, now whether it is social, Online education and so on, there will be a search bar implementation, but the effect of each other is not the same. The Search bar in iOS is relatively simple to implement, there are a lot of references on the Internet, but not much of it, many of them are implemented prior to iOS 8.0, the implementation on iOS 8.0 seems to be very rare to see, can run, but will see Searchdisplaycontroller ' is Deprecated:first deprecated in IOS 8.0 warning, read some of the foreigner's code, use a bit of uisearchcontroller feeling is very good.
Uisearchbar and Uisearchdisplaycontroller
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 using uisearchdisplaydelegate, the 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 through Uisearchdisplaydelegate to achieve the above effect is no problem, there are many similar implementation of the Internet, but the warning, the message is as follows: "Searchdisplaycontroller" is Deprecated:first deprecated in IOS 8.0, so obviously a warning can not be ignored, in StackOverflow found Uisearchdisplaycontroller is deprecated in IOS8.0, and recommended to use Uisearchcontroller instead, which means that iOS 8.0 does not recommend Uisearchdisplaycontroller, That is, the use of uisearchdisplaydelegate is not recommended, but can be achieved through the Uisearchcontroller implementation of uisearchresultsupdating 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:
Set number of rows in the range-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{ if ( self.searchController.active) { return [self.searchlist count]; } else{ return [self.datalist count]; } } Return cell contents-(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 (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:
But the effect of the two final results is basically the same.
Search bar Uisearchbar and Uisearchcontroller (Uisearchdisplaycontroller not recommended after iOS8.0)