1. Case study: Partition Table view with index 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> group A </key> <array> <string> A1-South Africa </string> <string> A2-Mexico </string> <string> A3-Uruguay </string> <string> A4-France </string> </array> <key> group B </key> <array> <string> B1-Argentina </string> <string> B2-Nigeria </string> <string> B3-Korea </string> <string> B4-Greece </string> </array> <key> group C </key> <array> <string> C1 -England </string> <string> C2-US </string> <string> C3-Algeria </string> <string> C4-Slovenia </string> </array> <key> group D </key> <array> <string> D1-Germany </string> <string> D2-Australia </string> <string> D3-Serbia </ string> <string> D4-Ghana </string> </array> <key> E group </key> <array> <string> E1-Netherlands </string> <string> E2-Denmark </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-Slovakia </string> <string> F4-New Zealand </string> </array> <key> G group </key> <array> <string> G1-Brazil </string> <string> G2-North Korea </string> <string> G3-Côte d'ivoire </string> <string> G4-Portugal </string> </array> <key> H group </key> <array> <string> H1 -Spain </string> <string> H2-Switzerland </string> <string> H3-Honduras </string> <string> H4-Chile </string> </array> </dict> </plist>
3.. h
# Import <UIKit/UIKit. h> @ interface CQ24ViewController: UITableViewController <UISearchBarDelegate> // table data @ property (strong, nonatomic) NSDictionary * dictData; // table data group name set @ property (strong, nonatomic) NSArray * listGroupname; @ end
4.. m
# Import "CQ24ViewController. h "@ interface CQ24ViewController () @ end @ implementation CQ24ViewController-(void) viewDidLoad {[super viewDidLoad]; required * mainBundle = [NSBundle mainBundle]; NSString * plistPath = [mainBundle pathForResource: @ "team_dictionary" ofType: @ "plist"]; // 1. initialize table data dictData self. dictData = [[NSDictionary alloc] initWithContentsOfFile: plistPath]; // 2. initialize the group name and sort NSArray * tempList = [self. dictData allKeys]; self. listGroupname = [tempList sortedArrayUsingSelector: @ selector (compare :)];}-(void) didreceivemorywarning {[super didreceivemorywarning];} # Number of groups returned by pragma mark-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {// 1. Obtain the group name NSString * groupName = [self. listGroupname objectAtIndex: section]; // 2. Use the group name as the key to retrieve the group data from the dictionary. NSArray * listTeams = [self. dictData objectForKey: groupName]; return [listTeams count];} # pragma mark fills in the Cell-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) of each section *) indexPath {// 1. Load a cell static NSString * CellIdentifier = @ "CellIdentifier"; UITableViewCell * cell = [tableView failed: CellIdentifier]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];} // 2. Obtain the selected section NSUInteger section = [indexPath section]; // 3. Obtain the node name NSString * groupName = [self. listGroupname objectAtIndex: section]; // 4. Obtain the section data NSArray * listTeams = [self. dictData objectForKey: groupName]; // 5. Obtain the row index NSUInteger row = [indexPath row] in the selected section; // 6. Set the text cell in the cell. textLabel. text = [listTeams objectAtIndex: row]; return cell ;}# pragma mark Number of sections-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {return self. listGroupname. count ;}# pragma mark Setting section title-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {NSString * groupName = [self. listGroupname objectAtIndex: section]; return groupName ;}# pragma mark index-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView {// 1. Set NSMutableArray * listTitles = [[NSMutableArray alloc] initWithCapacity: [self. listGroupname count]; // 2. initialize the index set for (NSString * item in self. listGroupname) {NSString * title = [item substringToIndex: 1]; [listTitles addObject: title];} return listTitles;} @ end