Phone Book index [Table view]

Source: Internet
Author: User
Tags allkeys

Mainviewcontroller. h

@interface RootViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>@property(nonatomic, retain)NSDictionary *dic;@property(nonatomic, retain)NSArray *allKeys;

Mainviewcontroller. m

-(Void) viewdidload {[Super viewdidload]; // create table view uitableview * tableview = [[uitableview alloc] initwithframe: cgrectmake (0, 20,320,460) style: uitableviewstyleplain]; tableview. delegate = self; tableview. datasource = self; [self. view addsubview: tableview]; [tableview release]; nsstring * Path = [[nsbundle mainbundle] pathforresource: @ "listdata. plist "oftype: Nil]; // listdata. plist _ DIC = [[nsdictionary alloc] initwithcontentsoffile: path]; // get key // _ allkeys = [_ DIC allkeys];/* sortedarrayusingselector: traverses every element in the array, call the following method */_ allkeys = [[[_ DIC allkeys] sortedarrayusingselector: @ selector (compare :)] retain]; // set the background color of the sidebar tableview. sectionindexbackgroundcolor = [uicolor blackcolor];}/* dictionary structure A: ["ASD", "ASE", "Aer", "add"], B: ["BSD ", "BSE", "Br", "BD"], C: ["cc", "cd"],... */# pragma mark-uitableview datasource // set the number of groups-(nsinteger) numberofsectionsintableview :( uitableview *) tableview {return _ dic. count;} // set the number of cells in each group-(nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {nsstring * Key = [_ allkeys objectatindex: section]; nsarray * value = [_ DIC objectforkey: Key]; return value. count;} // create cell-(uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {static nsstring * inde = @ "cell "; uitableviewcell * cell = [tableview metadata: inde]; If (cell = nil) {Cell = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: inde] autorelworkflow];} nsstring * Key = [_ allkeys objectatindex: indexpath. section]; nsarray * value = [_ DIC objectforkey: Key]; cell. textlabel. TEXT = value [indexpath. row]; return cell;} // set the group's header view title-(nsstring *) tableview :( uitableview *) tableview titleforheaderinsection :( nsinteger) Section {return [_ allkeys objectatindex: section];}-(nsarray *) sectionindextitlesfortableview :( uitableview *) tableview {return _ allkeys ;} // click the method called in the index column on the right. // The returned result is the group index-(nsinteger) tableview :( uitableview *) tableview sectionforsectionindextitle :( nsstring *) Title atindex :( nsinteger) index {nslog (@ "title: % @", title); nsinteger A = [_ allkeys indexofobject: title]; return a + 2 ;}


Phone Book index [Table view]

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.