iOS Uisearchdisplaycontroller implements UITableView search function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.