Self-implementation UITableView search, compared to the use of Uisearchdisplaycontroller to write a little trouble for themselves a little bit, but more flexible. The main is to use a field to distinguish between the current search or non-search, and then reload the corresponding data on the line, and the implementation of Uisearchdisplaycontroller is similar, but Uisearchdisplaycontroller is two TableView switch, here we are a tableview load different data.
Key code:
@interface Maintableviewcontroller:uiviewcontroller<uisearchbardelegate,uitableviewdelegate,uitableviewdatasource> { *mytableview; *data; *filterdata; // identifies whether a search is in progress UIView *mask;}
- (void) viewdidload{[Super Viewdidload]; Mytableview= [[UITableView alloc] Initwithframe:cgrectmake (0, -, Self.view.frame.size.width, self.view.frame.size.height- -)]; Mytableview.datasource=Self ; Mytableview.Delegate=Self ; [Self.view Addsubview:mytableview]; Uisearchbar*searchbar = [[Uisearchbar alloc] Initwithframe:cgrectmake (0,0, Self.view.frame.size.width, -)]; Searchbar.placeholder=@"Search"; Searchbar.Delegate=Self ; Mytableview.tableheaderview=Searchbar; //Add a maskmask = [[UIView alloc] Initwithframe:cgrectmake (0, -+ -, Self.view.frame.size.width, Self.view.frame.size.height- -- -)]; [Self.view Addsubview:mask]; Mask.backgroundcolor=[Uicolor Blackcolor]; Mask.alpha=0;}
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{//distinguish whether a search result set or the original result set is currently displayed by Isfiltered if(isfiltered) {returnFilterdata.count; } returnData.count;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *cellid =@"Cellid"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:cellid]; if(cell==Nil) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:cellid]; } //distinguish whether a search result set or the original result set is currently displayed by Isfiltered if(isfiltered) {Cell.textLabel.text=Filterdata[indexpath.row]; }Else{Cell.textLabel.text=Data[indexpath.row]; } returncell;}- (void) Searchbartextdidbeginediting: (Uisearchbar *) searchbar{//Pop mask When starting search and disable TableView clickNSLog (@"searchbartextdidbeginediting"); Isfiltered=YES; Searchbar.showscancelbutton=YES; Mask.alpha=0.3; Mytableview.allowsselection=NO; Mytableview.scrollenabled=NO;}- (void) Searchbartextdidendediting: (Uisearchbar *) searchbar{NSLog (@"searchbartextdidendediting");}- (void) Searchbar: (Uisearchbar *) Searchbar textdidchange: (NSString *) searchtext{NSLog (@"Textdidchange"); if(Searchtext.length = =0) {isfiltered=NO; Mask.alpha=0.3; Mytableview.allowsselection=NO; Mytableview.scrollenabled=NO; [Mytableview Reloaddata]; return; } isfiltered=YES; Mask.alpha=0; Mytableview.allowsselection=YES; Mytableview.scrollenabled=YES; //predicate SearchNspredicate *predicate = [Nspredicate predicatewithformat:@"Self contains [CD]%@", SearchText]; Filterdata=[[Nsmutablearray alloc] Initwitharray:[data Filteredarrayusingpredicate:predicate]]; [Mytableview reloaddata];}- (void) searchbarcancelbuttonclicked: (Uisearchbar *) sb{//Remove Mask When you click Cancel, ReloaddataSb.text =@""; [SB Setshowscancelbutton:no Animated:yes]; Mytableview.allowsselection=YES; Mytableview.scrollenabled=YES; [SB Resignfirstresponder]; Mask.alpha=0; Isfiltered=NO; [Mytableview reloaddata];}-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{NSString*text; if(isfiltered) {text=Filterdata[indexpath.row]; }Else{text=Data[indexpath.row]; } NSLog (@"You click index:%d%@", Indexpath.row,text);}
Wrote a DEMO:HTTP://PAN.BAIDU.COM/S/1NTN0MEP based on the ios7.1 layout
iOS UITableView Search