012 create an ios6.0 application on xcode4.5 (advanced control table view search box)

Source: Internet
Author: User
Tags allkeys
Advanced Control table view in IOS (add the search box to the table view)
In the previous blog, we implemented the table view control 011 and created the ios6.0 application on xcode4.5.
(Advanced control table view segment table view)
In the previous code, or based on the implementation of the search box, it is actually very simple, as long as you add a control search box there are two types, when selecting the control, we should try to select the following method because the following method has implemented the control and then add the searchbar Protocol to implement the two method protocols:

Uisearchbardelegate

Method:

-(Void) searchbarcancelbuttonclicked :( uisearchbar *) searchbar

-(Void) searchbar :( uisearchbar *) searchbar textdidchange :( nsstring *) searchtext

You can simply implement the effect of a search box. The detailed code viewcontroller. H will be posted below.

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>{    NSMutableDictionary *allteams;    NSMutableDictionary *teams;    NSArray *teamsname;}@property (nonatomic,retain)NSMutableDictionary *allteams;@property (nonatomic,retain)NSMutableDictionary *teams;@property (nonatomic,retain)NSArray *teamsname;-(void)resetSearch;@end

Viewcontroller. m

@ Implementation viewcontroller @ synthesize allteams; @ synthesize teams; @ synthesize teamsname; // re-search-(void) resetsearch {self. teams = self. allteams; nsmutablearray * keyarray = [[nsmutablearray alloc] init]; [keyarray addobjectsfromarray: [teams allkeys] sortedarrayusingselector: @ selector (compare :)]; self. teamsname = keyarray; [keyarray release];} // load data-(void) viewdidload {[Super viewdidload]; // The following is the mode generation Code to read the file to the code. nsbundle * bundle = [nsbundle mainbundle]; nsstring * filepath = [bundle pathforresource: @ "statedictionary" oftype: @ "plist"]; nsmutabledictionary * DIC = [[nsmutabledictionary alloc] initwithcontentsoffile: filepath]; self. allteams = DIC; [DIC release]; [self resetsearch];} // load the data source-(nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) section {// The number of lines in the return segment nsstring * Nam E = [teamsname objectatindex: section]; nsarray * team = [teams objectforkey: Name]; return [team count];} // return quantity-(nsinteger) numberofsectionsintableview :( uitableview *) tableview {return [teamsname count];} // return the name in each segment-(nsstring *) tableview :( uitableview *) tableview titleforheaderinsection :( nsinteger) section {nsstring * name = [teamsname objectatindex: section]; return name;} // mode code to fill in data-(uitableviewcell *) Tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {static nsstring * cellidentifier = @ "cell"; uitableviewcell * cell = [tableview progress: cellidentifier]; if (cell = nil) {Cell = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cellidentifier] autorelease];} nsuinteger section = [indexpath section]; nsui Nteger ROW = [indexpath row]; nsstring * name = [teamsname objectatindex: section]; nsarray * team = [teams objectforkey: Name]; // return protocol title cell. textlabel. TEXT = [Team objectatindex: Row]; return cell;} // index implementation-(nsarray *) sectionindextitlesfortableview :( uitableview *) tableview {return teamsname ;} // implement the method to represent the graph-(void) tableview :( uitableview *) tableview didselectrowatindexpath :( nsindexpath *) indexpath {nsuinteger Section = [indexpath section]; nsuinteger ROW = [indexpath row]; nsstring * name = [teamsname objectatindex: section]; nsarray * team = [teams objectforkey: Name]; nsstring * selectedteam = [Team objectatindex: Row]; nsstring * message = [[nsstring alloc] initwithformat: @ "your selected number is % @", selectedteam]; uialertview * Alert = [[uialertview alloc] initwithtitle: @ "team selection" message: Message delegate: Self cancelbuttont Itle: @ "OK" otherbuttontitles: nil, nil]; [alert show]; [alert release]; [Message release]; // when clicking, let the selected one slowly disappear [tableview deselectrowatindexpath: indexpath animated: Yes];} // query method-(void) searchbarcancelbuttonclicked :( uisearchbar *) searchbar {[self resetsearch];} -(void) searchbar :( uisearchbar *) searchbar textdidchange :( nsstring *) searchtext {If ([searchtext length] = 0) {[self resetsearch]; return ;} Nsmutabledictionary * dict = [[nsmutabledictionary alloc] init]; for (nsstring * key in self. allteams) {nsmutablearray * arry = [allteams valueforkey: Key]; nsmutablearray * newteams = [[nsmutablearray alloc] init]; for (nsstring * teamname in arry) {If ([teamname rangeofstring: searchtext options: nscaseinsensitivesearch]. location! = Nsnotfound) {[newteams addobject: teamname] ;}} if ([newteams count]> 0) {[dict setobject: newteams forkey: Key];} [newteams release];} self. teamsname = [[dict allkeys] sortedarrayusingselector: @ selector (compare :)]; self. teams = dict; [dict release];}-(void) didreceivememorywarning {[Super didreceivemorywarning]; // dispose of any resources that can be recreated .} -(void) dealloc {[allteams release]; [teams release]; [teamsname release]; [Super dealloc];} @ end

Last look

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.