1. Case Description: A Sub-section table view with indexing function, 01
Figure 01
2, Team_dictionary.plist
<?xml version="1.0"encoding="UTF-8"? ><! DOCTYPE plist Public"-//apple//dtd PLIST 1.0//en" "HTTP://WWW.APPLE.COM/DTDS/PROPERTYLIST-1.0.DTD"><plist version="1.0"><dict> <key>a Group </key> <array> <string>a1-South Africa </string> <string>a2-Mexico </string> <string>a3-Uruguay </string> <string>a4-French </string> </array> <key>b Group </key> <array> <string>b1-Argentine </string> <string>b2-Nigerian </string> <string>b3-Korea </string> <string>b4-Greek </string> </array> <key>c Group </key> <array> <string>c1-England </string> <string>c2-American </string> <string>c3-Algeria </string> <string>c4-Slovenia </string> </array> <key>d Group </key> <array> <string>d1-Germany </string> <string>d2-Australia </string> <string>d3-Serbian </string> <string>d4-Ghana </string> </array> <key>e Group </key> <array> <string>e1-Holland </string> <string>e2-Danish </string> <string>e3-Japan </string> <string>e4-Cameroon </string> </array> <key>f Group </key> <array> <string>f1-Italy </string> <string>f2-Paraguay </string> <string>f3-Slovak </string> <string>f4-New Zealand </string> </array> <key>g Group </key> <array> <string>g1-Brazil </string> <string>g2-Korea </string> <string>g3-Côte d ' Ivoire </string> <string>g4-Portuguese </string> </array> <key>h Group </key> <array> <string>h1-Spain </string> <string>h2-Swiss </string> <string>h3-Honduras </string> <string>h4-Chilean </string> </array></dict></plist>
3,. h
#import <UIKit/UIKit.h>@interface cq24viewcontroller:uitableviewcontroller< Uisearchbardelegate>// table Data @property (strong,nonatomic) nsdictionary *dictdata; // table Data grouping name Collection @property (strong,nonatomic) nsarray *listgroupname; @end
4,. m
#import "CQ24ViewController.h"@interfaceCq24viewcontroller ()@end@implementationCq24viewcontroller- (void) viewdidload{[Super Viewdidload]; NSBundle*mainbundle =[NSBundle Mainbundle]; NSString*plistpath = [Mainbundle pathforresource:@"team_dictionary"OfType:@"plist"]; //1. Initialize Tabular data dictdataSelf.dictdata =[[Nsdictionary alloc] initwithcontentsoffile:plistpath]; //2. Initialize group name, sortNsarray *templist =[Self.dictdata AllKeys]; Self.listgroupname=[templist sortedarrayusingselector: @selector (compare:)]; }- (void) didreceivememorywarning{[Super didreceivememorywarning];}#pragmaMark returns the number of groups-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{//1. Get the group nameNSString *groupname =[Self.listgroupname objectatindex:section]; //2. Take the group name as key and remove the grouped data from the dictionaryNsarray *listteams =[Self.dictdata Objectforkey:groupname]; return[Listteams Count]; }#pragmaMark fills each section's cell-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//1. Load a cell StaticNSString *cellidentifier =@"Cellidentifier"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:cellidentifier]; if(Cell = =Nil) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } //2. Get the selected sectionNsuinteger section =[Indexpath section]; //3. Get the section nameNSString *groupname =[Self.listgroupname objectatindex:section]; //4. Get the section dataNsarray *listteams =[Self.dictdata Objectforkey:groupname]; //5. Get the row index in the selected sectionNsuinteger row =[Indexpath Row]; //6. Set the text in the cellCell.textLabel.text =[Listteams Objectatindex:row]; returncell;}#pragmaMark Number of Knots-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{returnSelf.listGroupname.count;}#pragmaMark set Section title-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) section{nsstring*groupname =[Self.listgroupname objectatindex:section]; returnGroupName;}#pragmaMark Index-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) tableview{//1. The set of indexes that existNsmutablearray *listtitles =[[Nsmutablearray alloc] initwithcapacity:[self.listgroupname Count]]; //2. Initialize the Index collection for(NSString *iteminchself.listgroupname) {nsstring*title = [item Substringtoindex:1]; [Listtitles Addobject:title]; } returnlisttitles;}@end