How to add Uisearchbar and Uisearchdisplaycontroller using--oc--ios on Uinavigationbar

Source: Internet
Author: User

So let's start, here's a demo from sely, to share with you.

To create a new project, Uisearchdisplaycontroller's displayssearchbarinnavigationbar is too rigid to achieve the desired effect. here to re-customize, four protocols, three member variables, the first step OK.

@interface Viewcontroller () <uisearchbardelegate,uisearchdisplaydelegate, Uitableviewdatasource, Uitableviewdelegate>

{

Uisearchbar *mysearchbar;

Uisearchdisplaycontroller *mysearchdisplaycontroller;

Nsmutablearray *suggestresults;

}

@end

Let's initialize our three members.

-(void) Initmysearchbarandmysearchdisplay

{

Mysearchbar = [[Uisearchbar alloc] Initwithframe:cgrectmake (0, [uiapplicationsharedapplication]. StatusBarFrame.size.height, [UIScreen Mainscreen].bounds.size.width, 44)];

Mysearchbar.delegate = self;

Setting options

Mysearchbar.bartintcolor = [Uicolor Redcolor];

Mysearchbar.searchbarstyle = Uisearchbarstyledefault;

Mysearchbar.translucent = NO; Whether translucent

[Mysearchbar Setautocapitalizationtype:uitextautocapitalizationtypenone];

[Mysearchbar SizeToFit];

Mysearchdisplaycontroller = [[Uisearchdisplaycontroller alloc] Initwithsearchbar:mysearchbar contentsController: Self];

Mysearchdisplaycontroller.delegate = self;

Mysearchdisplaycontroller.searchresultsdatasource = self;

Mysearchdisplaycontroller.searchresultsdelegate = self;

Suggestresults = [Nsmutablearray arraywitharray:@[@ "Here is the recommended word, @" can also be history "]];

}

And then, of course, we're customizing our navigation bar.

-(void) Initnavigationbar

{

UIButton *morebutton = [[UIButton alloc] Initwithframe:cgrectmake (0, 0, 30, 30)];

[Morebutton setimage:[uiimage imagenamed:@ "More"] forstate:uicontrolstatenormal];

[Morebutton addtarget:self Action: @selector (Handlemore:) forcontrolevents:uicontroleventtouchupinside];

Uibarbuttonitem *moreitem = [[Uibarbuttonitem alloc] Initwithcustomview:morebutton];

UIButton *searchbutton = [[UIButton alloc] Initwithframe:cgrectmake (0, 0, 30, 30)];

[SearchButton setimage:[uiimage imagenamed:@ "Info_search"] forstate:uicontrolstatenormal];

[SearchButton addtarget:self Action: @selector (Startsearch:) forcontrolevents:uicontroleventtouchupinside];

Uibarbuttonitem *searchitem = [[Uibarbuttonitem alloc] Initwithcustomview:searchbutton];

Self.navigationItem.rightBarButtonItems = @[moreitem, Searchitem];

}

Okay, start loading the interface.

-(void) viewdidload

{

[Super Viewdidload];

[Self initnavigationbar];

[Self initmysearchbarandmysearchdisplay];

}

Implement our Click events

#pragma mark-handle button click

-(void) Startsearch: (ID) sender

{

[Self.navigationController.view Addsubview:mysearchbar];

[Mysearchbar Becomefirstresponder];

}

-(void) Handlemore: (ID) sender

{}

//Implement our Table view protocol to determine when we start the search and go to a searchdisplaycontroller method of our own implementation, so that the code seems to be not very concise?

#pragma Mark-uitableviewdatasource & Uitableviewdelegate

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section

{

if (TableView = = Mysearchdisplaycontroller.searchresultstableview)

{

return [self numberofrowswithsearchresulttableview];

}

return 0;

}

-(uitableviewcell*) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath

{

if (TableView = = Mysearchdisplaycontroller.searchresultstableview)

{

return [self SearchTableView:mySearchDisplayController.searchResultsTableView cellforrowatindexpath:indexpath];

}

Else

{

static NSString *cellid = @ "Search_cell";

UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellid];

if (cell = = nil)

{

cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:cellid];

}

return cell;

}

}

This is dedicated to Searchresultstableview data source and delegate in Uisearchdisplaycontroller.

#pragma mark-uisearchdisplaycontroller <UITableViewDataSource> DataSource

-(Nsinteger) Numberofrowswithsearchresulttableview

{

return suggestresults.count + 1;

}

-(uitableviewcell*) Searchtableview: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath

{

static NSString *suggestid = @ "Suggestcell";

UITableViewCell *cell = [TableView Dequeuereusablecellwithidentifier:suggestid];

if (cell = = nil)

{

cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:suggestid];

Cell.accessorytype = Uitableviewcellaccessorydisclosureindicator;

}

if (Indexpath.row = = Suggestresults.count)

{

Cell.textLabel.text = [Nslocalizedstring (@ "Search:", @ "find:") StringByAppendingString:mySearchBar.text];

}

Else

{

Cell.textLabel.text = [Suggestresults objectAtIndex:indexPath.row];

}

return cell;

}

#pragma mark-uisearchdisplaycontroller <UITableViewDelegate> Delegate

-(void) Searchtableview: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath

{

NSString *keyword = nil;

if (Indexpath.row = = Suggestresults.count)

{

keyword = Mysearchbar.text;

}

Else

{

Keyword = [suggestresults objectAtIndex:indexPath.row];

}

[Mysearchbar Resignfirstresponder];

}

Start our Search bar delegate, enter the content, because it is the demo, so I do not have search results, this people according to the needs of their own implementation of it

#pragma mark-uisearchbardelegate

-(BOOL) searchbarshouldbeginediting: (Uisearchbar *) Searchbar

{

return YES;

}

-(void) searchbarsearchbuttonclicked: (Uisearchbar *) Searchbar

{

}

-(void) Searchbar: (Uisearchbar *) Searchbar textdidchange: (NSString *) SearchText

{

[Mysearchdisplaycontroller.searchresultstableview Reloaddata];

}

The last part, the logic of this place will be changeable.

#pragma mark-uisearchdisplaydelegate

-(void) Searchdisplaycontrollerdidbeginsearch: (Uisearchdisplaycontroller *) controller Ns_deprecated_ios (3_0,8_0)

{

Start Search events, set Searchresultstableview Contentinset, otherwise the location will be wrong

MySearchDisplayController.searchResultsTableView.contentInset = Uiedgeinsetsmake (mySearchBar.frame.size.height, 0 , 0, 0);

}

-(void) Searchdisplaycontroller: (Uisearchdisplaycontroller *) controller Didloadsearchresultstableview: ( UITableView *) Tableviewns_deprecated_ios (3_0,8_0)

{

Load Searchresultstableview Complete

}

-(void) Searchdisplaycontrollerdidendsearch: (Uisearchdisplaycontroller *) controller Ns_deprecated_ios (3_0,8_0)

{

End Search event, remove Mysearchbar

[Mysearchbar Removefromsuperview];

}

Return YES to reload table. Called when search string/option changes. Convenience methods on top Uisearchbar delegate methods

-(BOOL) Searchdisplaycontroller: (Uisearchdisplaycontroller *) controller shouldreloadtableforsearchstring: ( nsstring*) searchstring Ns_deprecated_ios (3_0,8_0)

{

Whether you need to refresh Searchresultstableview

if (@ "Meet the Criteria")

{

return YES;

}

return NO;

}

Is it easy to end up here? This is the first release, if you do not good, please the big God, if the writing is not good (estimate is affirmative), please give more advice, and finally hope to help you a lot.

sely Original

How to add Uisearchbar and Uisearchdisplaycontroller using--oc--ios on Uinavigationbar

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.