Add an index for IOS)

Source: Internet
Author: User

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 !!!!

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.