Uisearchdisplaycontroller is a class that Apple specifically encapsulates for the UITableView search.
There is a built-in UITableView to display the results of the search. It can be used with a search function to
Controller Association, other like the original TableView and search results TableView switch, mask display, etc. are
The package is good and very simple to use. In particular, to achieve full-screen search use the most.
Full Screen search means that if you use Navigationbar when you click on the search box, TableView will automatically pop up to cover
Navigationbar, to achieve a full-screen search effect, all uisearchdisplaycontroller are encapsulated, if you
Writing is a little more troublesome.
Key code:
@interface mainviewcontroller:uitableviewcontroller{ *data; *filterdata; *Searchdisplaycontroller;}
- (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 ;}
/** 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;}
iOS Uisearchdisplaycontroller implements UITableView search function