Add an index for IOS)
Indexes are used to assist in queries.
Principles:
-The index title cannot be exactly the same as the displayed title;
-Indexes should be representative and can represent a data set;
-If the index list view is used, the extended view is generally no longer used. (Easy to use)
Method of the data source to be retransmitted:
TableView: numberOfRowsInSection: ------ get the number of rows in a section
TableView: cellForRowAtIndexPath: ------- Cell data Implementation
NumberofSectionInTableView: ---------- get the number of segments
TableView: titleForHeaderInSection: -------- section title
SectionIndexTitlesForTableView: -------- obtain the index
@ Implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; NSBundle * bundle = [NSBundle mainBundle]; NSString * plistPath = [bundle pathForResource: @ "team_dictionary" ofType: @ "plist"]; // obtain all data in the property list file self. dicData = [[NSDictionary alloc] initWithContentsOfFile: plistPath]; NSArray * tempList = [self. dicData allKeys]; // sort keys. // selector is of the SEL type. The parameter of sortedArrayUsingSelector must be selecto. R self. listGroupname = [tempList sortedArrayUsingSelector: @ selector (compare :)];}-(void) didreceivemorywarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} # pragma mark data source Rewriting Method-(NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection :( NSInteger) section {// obtain the group name NSString * groupName = [self. listGroupname objectAtIndex: section]; // set The group name is used as the key, and the team array set is retrieved from the dictionary // because the storage is a hash structure NSArray * listTeams = [self. dicData objectForKey: groupName]; // total number of teams in this section return [listTeams count];}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * CellIdentifier = @ "CellIdentifier"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; if (cell = nil) {c Ell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: CellIdentifier];} // obtain the selected section. Group A is section 1st NSUInteger section = [indexPath section]; // obtain the row indexes in Group A (Group A, Group 1st, and Group 2nd... NSUInteger row = [indexPath row]; // obtain the group name from the group name array according to the Section index -- group A NSString * groupName = [self. listGroupname objectAtIndex: section]; // uses the group name as the key and retrieves the team array set NSArray * listTeams = [self. dicData objectForKey: groupName]; cell. textLabel. text = [listTeams objectAtIndex: row]; return cell;}-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {// return [self. listGroupname count];}-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {// The section header NSString * groupName = [self. listGroupname objectAtIndex: section]; return groupName;}-(NSArray *) Partition :( UITableView *) tableView {// Add index NSMutableArray * listTitles = [[NSMutableArray alloc] parts: [self. listGroupname count]; // Change Group A to A for (NSString * item in self. listGroupname) {NSString * title = [item substringToIndex: 1]; // only obtain the first character [listTitles addObject: title]; // The last position of the appended string array} return listTitles;} @ end
Note:
Configure the tableview delegation protocol in the Story version !!!!