Use of IOS Uisearchcontroller

Source: Internet
Author: User

In IOS9, Uisearchdisplaycontroller has been replaced by Uisearchcontroller. The search box is a common control.

Suppose we want to meet the need to produce 100 "numbers + three random letters" and then search for results that contain a letter.

So, what should we do?

#import "ViewController.h"@interfaceViewcontroller () <uitableviewdelegate,uitableviewdatasource,uisearchcontrollerdelegate, Uisearchresultsupdating>//TableView@property (Strong, nonatomic) UITableView *TableView;//Searchcontroller@property (Strong, nonatomic) Uisearchcontroller *Searchcontroller;//Data Source@property (strong,nonatomic) Nsmutablearray *dataList, @property (strong,nonatomic) Nsmutablearray*searchlist;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; _datalist=[Nsmutablearray array]; _searchlist=[Nsmutablearray array]; Self.datalist=[nsmutablearray arraywithcapacity: -]; //produces 100 "numbers + three random letters"     for(Nsinteger i=0; i< -; i++) {[Self.datalist addobject:[nsstring stringWithFormat:@"%ld%@",(Long) i,[self Shuffledalphabet]]; } _tableview= [[UITableView alloc]initwithframe:cgrectmake (0, -, [UIScreen mainscreen].bounds.size.width, [UIScreen Mainscreen].bounds.size.height]]; _tableview.Delegate=Self ; _tableview.datasource=Self ; _tableview.separatorstyle=Uitableviewcellselectionstylenone; //Create Uisearchcontroller_searchcontroller =[[Uisearchcontroller Alloc]initwithsearchresultscontroller:nil]; //Set up proxy_searchcontroller.Delegate=Self ; _searchcontroller.searchresultsupdater=Self ; //set the display properties of the Uisearchcontroller, the following 3 properties default to Yes//Dim color Background when searching_searchcontroller.dimsbackgroundduringpresentation =NO; //when searching, the background becomes blurred_searchcontroller.obscuresbackgroundduringpresentation =NO; //Hide Navigation bar_searchcontroller.hidesnavigationbarduringpresentation =NO; _searchcontroller.searchbar.frame= CGRectMake (self.searchcontroller.searchbar.frame.origin.x, SELF.SEARCHCONTROLLER.SEARCHBAR.FRAME.ORIGIN.Y, Self.searchController.searchBar.frame.size.width,44.0); //add Searchbar to HeaderviewSelf.tableView.tableHeaderView =_searchcontroller.searchbar;            [Self.view Addsubview:_tableview]; //additional setup after loading the view, typically from a nib.}//generate 3 Random letters-(NSString *) Shuffledalphabet {Nsmutablearray* Shuffledalphabet = [Nsmutablearray arraywitharray:@[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"]]; NSString*strtest =[[NSString Alloc]init];  for(intI=0; i<3; i++) {        intx = arc4random ()% -; Strtest= [NSString stringWithFormat:@"%@%@", Strtest,shuffledalphabet[x]]; }        returnstrtest; }//set the number of rows in a range-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{if(self.searchController.active) {return[self.searchlist Count]; }Else{        return[self.datalist Count]; }}//return cell contents-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *flag=@"Cell"; UITableViewCell*cell=[TableView Dequeuereusablecellwithidentifier:flag]; if(cell==Nil) {Cell=[[UITableViewCell Alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:flag]; }    if(self.searchController.active) {[Cell.textlabel settext:self.searchlist[indexpath.row]]; }    Else{[Cell.textlabel settext:self.datalist[indexpath.row]]; }    returncell;}#pragmaMark-uisearchcontrollerdelegate Agent//test the execution of Uisearchcontroller- (void) Willpresentsearchcontroller: (Uisearchcontroller *) searchcontroller{NSLog (@"Willpresentsearchcontroller");}- (void) Didpresentsearchcontroller: (Uisearchcontroller *) searchcontroller{NSLog (@"Didpresentsearchcontroller");}- (void) Willdismisssearchcontroller: (Uisearchcontroller *) searchcontroller{NSLog (@"Willdismisssearchcontroller");}- (void) Diddismisssearchcontroller: (Uisearchcontroller *) searchcontroller{NSLog (@"Diddismisssearchcontroller");}- (void) Presentsearchcontroller: (Uisearchcontroller *) searchcontroller{NSLog (@"Presentsearchcontroller");}-(void) Updatesearchresultsforsearchcontroller: (Uisearchcontroller *) Searchcontroller {NSLog (@"Updatesearchresultsforsearchcontroller"); NSString*searchstring =[Self.searchController.searchBar text]; Nspredicate*preicate = [Nspredicate predicatewithformat:@"Self contains[c]%@", SearchString]; if(self.searchlist!=Nil)    {[Self.searchlist removeallobjects]; }    //Filtering Dataself.searchlist=[Nsmutablearray arraywitharray:[_datalist filteredarrayusingpredicate:preicate]; //Refresh Table[Self.tableview reloaddata];}

Uisearchcontroller The execution process of the Proxy method:

Uisearchcontroller steps to use:1. Create
// Create Uisearchcontroller    _searchcontroller = [[Uisearchcontroller Alloc]initwithsearchresultscontroller:nil];

2, set up the agent
    // set up proxy    _searchcontroller. delegate = self ;    _searchcontroller.searchresultsupdater= self;

3. Setting properties
    // set the display properties of the Uisearchcontroller, the following 3 properties default    to Yes // Dim color    Background when searching _searchcontroller.dimsbackgroundduringpresentation = NO;     // when searching, the background becomes blurred    _searchcontroller.obscuresbackgroundduringpresentation = NO;     // Hide Navigation bar    _searchcontroller.hidesnavigationbarduringpresentation = NO;

4. Implement the Agent
-(void) Willpresentsearchcontroller: (Uisearchcontroller *) Searchcontroller; -(void) Didpresentsearchcontroller: (Uisearchcontroller *) Searchcontroller; -(void) Willdismisssearchcontroller: (Uisearchcontroller *) Searchcontroller; -(void) Diddismisssearchcontroller: (Uisearchcontroller *) Searchcontroller; -(void) Presentsearchcontroller: (Uisearchcontroller *) Searchcontroller; -(void) Updatesearchresultsforsearchcontroller: (Uisearchcontroller *) Searchcontroller;

Note the point:

1. If you want to display search results in the same view, [[Uisearchcontroller Alloc]initwithsearchresultscontroller:nil]. But this is not supported tvOS, please provide tvos be sure to specify the result controller.

[[Uisearchcontroller ALLOC]INITWITHSEARCHRESULTSCONTROLLER:VC], you can implement the specified result controller.

Use of IOS Uisearchcontroller

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.