Use of search box Uisearchbar and search methods in iOS

Source: Internet
Author: User

The search box can be uisearchbar, and proxy uisearchbardelegate can be set.

1-(void) addsearchbar{2CGRect Searchbarrect = CGRectMake (0,0, Self.view.frame.size.width, -);3Uisearchbar *searchbar =[[Uisearchbar alloc]initwithframe:searchbarrect];4Searchbar.placeholder =@"Plrase Enter key word";5Self.tableView.tableHeaderView =Searchbar;6     //Set the Cancel button next to the search box7Searchbar.showscancelbutton =YES;8     //Set the search scope9Searchbar.showsscopebar =YES;TenSearchbar.scopebuttontitles = [Nsarray arraywithobjects:@"name",@"Sex",@"School Number", nil]; One     //Agent ASearchbar.Delegate=Self ; -}

Effect

The processing of the result search is also not very cumbersome, the principle is whether the string contains search keywords, and then refresh TableView

// contact.firstname for all data, keyword for search keywords if ([contact.firstName.uppercaseString containsString:keyWord.uppercaseString]) {    [_searchcontacts addobject : contact];}

Implementing proxies can be used

#pragma mark search box agent #pragma Mark Cancel Search-(void) searchbarcancelbuttonclicked: (Uisearchbar * ) searchbar{    // Discard First Responder object, turn off soft keyboard     [Self.searchbar resignfirstresponder];} #pragma Mark Enter search keywords-(void) Searchbar: (Uisearchbar *) Searchbar textdidchange: (NSString *) searchtext{    }#pragma mark clicks Search on the virtual keyboard-(void) searchbarsearchbuttonclicked: ( Uisearchbar *) searchbar{    [Self.searchbar resignfirstresponder];}

The effect is as follows:

can also use a simpler uisearchdisplaycontroller, it has a UITableView type of object Searchresultstableview, you can not use the top 3 agents. You only need to implement 1. Uisearchdisplaycontroller can also be automatically full-screen when using Uinavavigationcontroller, the effect is

#pragma Mark-uisearchdisplaycontroller Proxy Method-(BOOL) Searchdisplaycontroller: (Uisearchdisplaycontroller *) Controller shouldreloadtableforsearchstring: (NSString *) searchstring{    [self Searchdatawithkeyword: SearchString];     return YES;}

When adding Uisearchdisplaycontroller, there are two ways to do it. Need to be aware, otherwise it will be reported
Property ' Searchdisplaycontroller ' attempting-use instance variable ' _searchdisplaycontroller ' declared in Super class ' Uiviewcontroller '
The first, in the form of property, requires @synthesize modification to

@interfaceContactstableviewcontroller () <UISearchBarDelegate,UISearchDisplayDelegate>@property (nonatomic,strong) Uisearchbar*Searchbar, @property (nonatomic,strong) Uisearchdisplaycontroller*Searchdisplaycontroller;@end@implementationContactstableviewcontroller//need to be decorated with synthesize@synthesizeSearchdisplaycontroller;//the Add search bar at this point can be used-(void) addsearchbar{_searchbar=[[Uisearchbar alloc]init]; [_searchbar SizeToFit];//Size Adaptive Container_searchbar.placeholder =@"Plrase Enter key word"; _searchbar.autocapitalizationtype=Uitextautocapitalizationtypenone; _searchbar.showscancelbutton= YES;//Show Cancel button_searchbar.Delegate=Self ; Self.tableView.tableHeaderView=_searchbar; Self.searchdisplaycontroller=[[Uisearchdisplaycontroller Alloc]initwithsearchbar:self.searchbar contentscontroller:self]; Self.searchdisplaycontroller.Delegate=Self ; Self.searchDisplayController.searchResultsDataSource=Self ; Self.searchDisplayController.searchResultsDelegate=Self ; [Self.searchdisplaycontroller Setactive:no animated:yes];}@end

The second, with an underlined form, without @synthesize modification

@interfaceContactstableviewcontroller () <UISearchBarDelegate,UISearchDisplayDelegate>{Uisearchdisplaycontroller*_searchdisplaycontroller;} @property (nonatomic,strong) Uisearchbar*Searchbar;@end@implementationContactstableviewcontroller//at this point, add the search bar to-(void) addsearchbar{Self.searchbar=[[Uisearchbar alloc]init]; [Self.searchbar SizeToFit];//Size Adaptive ContainerSelf.searchBar.placeholder =@"Plrase Enter key word"; Self.searchBar.autocapitalizationType=Uitextautocapitalizationtypenone; Self.searchBar.showsCancelButton= YES;//Show Cancel buttonSelf.searchbar.Delegate=Self ; Self.tableView.tableHeaderView=Self.searchbar; _searchdisplaycontroller=[[Uisearchdisplaycontroller Alloc]initwithsearchbar:self.searchbar contentscontroller:self]; _searchdisplaycontroller.Delegate=Self ; _searchdisplaycontroller.searchresultsdatasource=Self ; _searchdisplaycontroller.searchresultsdelegate=Self ; [_searchdisplaycontroller Setactive:no animated:yes];}@end

Use of search box Uisearchbar and search methods in iOS

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.