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]