IOS8 after we can directly use the Uisearchcontroller agent method for development, no longer uisearch and other things, I will directly to everyone on the code bar
Uisearchbardelegate,uisearchresultsupdating these two proxy methods
Viewdidload:
Self.datalist=[nsmutablearray arraywithcapacity: -]; for(Nsinteger i=0; i< -; i++) {[Self.datalist addobject:[nsstring stringWithFormat:@"%ld-flyelephant",(Long) [i]]; } ALog (@"-====%lu", (unsignedLong) self.dataList.count); TableView=[[UITableView alloc]initwithframe:self.view.bounds Style:uitableviewstyleplain]; TableView.Delegate=Self ; Tableview.datasource=Self ; [Self.view Addsubview:tableview]; _search=[[Uisearchcontroller Alloc]initwithsearchresultscontroller:nil]; _search.searchresultsupdater=Self ; _search.dimsbackgroundduringpresentation=NO; _search.hidesnavigationbarduringpresentation=NO; _search.searchbar.frame= CGRectMake (_search.searchbar.frame.origin.x, _SEARCH.SEARCHBAR.FRAME.ORIGIN.Y, _ Search.searchBar.frame.size.width, -);
As the head view of the table Tableview.tableheaderview= _search.searchbar;
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{
Be sure to judge the stateif(_search.active) {return[self.searchlist Count]; } Else{ return[self.datalist Count]; }}-(uitableviewcell*) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *flag=@"Cellflag"; UITableViewCell*cell=[TableView Dequeuereusablecellwithidentifier:flag]; if(cell==Nil) {Cell=[[UITableViewCell Alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:flag]; } if(_search.active) {[Cell.textlabel settext:self.searchlist[indexpath.row]]; } Else{[Cell.textlabel settext:self.datalist[indexpath.row]]; } returncell; }-(void) Updatesearchresultsforsearchcontroller: (Uisearchcontroller *) searchcontroller{NSString*searchstring =[_search.searchbar text];
Filter Statement Nspredicate*predicate =[nspredicate Predicatewithformat:@"Self contains[c]%@", SearchString]; if(Self.searchlist! =Nil) {[self. searchlist removeallobjects]; } self.searchlist=[Nsmutablearray arraywitharray:[self.datalist filteredarrayusingpredicate:predicate]; ALog (@"-==%ld", Self.searchList.count); [TableView Reloaddata]; }
iOS Development---simple search