////MainViewController.h//Uisearchdisplaycontrollerdemo////Created by Enwaysoft on 14-8-20.//Copyright (c) 2014 Enway. All rights reserved.//#import<UIKit/UIKit.h>@interfacemainviewcontroller:uitableviewcontroller{Nsarray*data; Nsarray*Filterdata; Uisearchdisplaycontroller*Searchdisplaycontroller;}@end
////MAINVIEWCONTROLLER.M//Uisearchdisplaycontrollerdemo////Created by Enwaysoft on 14-8-20.//Copyright (c) 2014 Enway. All rights reserved.//#import "MainViewController.h"@interfaceMainviewcontroller ()@end@implementationMainviewcontroller- (ID) Initwithstyle: (uitableviewstyle) style{ Self=[Super Initwithstyle:style]; if(self) {data= [Nsarray arraywithobjects:@"Allan",@"abbbb",@"ACCCC",@"BCCCCC",@"CDDDDFFK",@"cddkllll",@"EKKFLFL",@"EKLJLJFG",@"Leslie",@"Mm",@"MEINV",@"Meihi",@"Catilin",@"Arron", nil]; } returnSelf ;}- (void) viewdidload{[Super Viewdidload]; Uisearchbar*searchbar = [[Uisearchbar alloc] Initwithframe:cgrectmake (0,0, Self.view.frame.size.width, -)]; Searchbar.placeholder=@"Search"; //add Searchbar to HeaderviewSelf.tableView.tableHeaderView =Searchbar; //Initialize Searchdisplaycontroller with Searchbar//and associate the Searchdisplaycontroller with the current controller.Searchdisplaycontroller =[[Uisearchdisplaycontroller alloc] Initwithsearchbar:searchbar contentscontroller:self]; //Searchresultsdatasource is Uitableviewdatasource .Searchdisplaycontroller.searchresultsdatasource =Self ; //searchresultsdelegate is Uitableviewdelegate .Searchdisplaycontroller.searchresultsdelegate =Self ;}- (void) didreceivememorywarning{[Super didreceivememorywarning];}#pragmaMark-table View Data source-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return 1;}/** If the delete of TableView in the original TableView and Searchdisplaycontroller points to the same object * it is necessary to distinguish between the current TableView in the callback .*/-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{if(TableView = =Self.tableview) {returnData.count; }Else{ //predicate SearchNspredicate *predicate = [Nspredicate predicatewithformat:@"Self contains [CD]%@", SearchDisplayController.searchBar.text]; Filterdata=[[Nsarray alloc] Initwitharray:[data Filteredarrayusingpredicate:predicate]]; returnFilterdata.count; }}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *cellid =@"MyCell"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:cellid]; if(Cell = =Nil) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:cellid]; } if(TableView = =self.tableview) {Cell.textLabel.text=Data[indexpath.row]; }Else{Cell.textLabel.text=Filterdata[indexpath.row]; } returncell;}-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{NSString*text; if(TableView = =self.tableview) {text=Data[indexpath.row]; }Else{text=Filterdata[indexpath.row]; } NSLog (@"You click%d%@", Indexpath.row,text);}@end
[Iosdemo search box]