Add index (iOS development)

Source: Internet
Author: User

Indexes are used to aid queries.

Principle:

-the index title cannot be exactly the same as the caption displayed;

-The index should be representative and representative of a data set;

-If an indexed list view is used, the extended view is not normally used. (easy to point to)

The data source method that will be re-used:

Tableview:numberofrowsinsection:------Gets the number of rows in a section

Tableview:cellforrowatindexpath: Implementation of-------cell data

Numberofsectionintableview:----------Get the number of sections

Tableview:titleforheaderinsection:--------section title

Sectionindextitlesfortableview:--------Get Index




@implementation viewcontroller-(void) viewdidload {[Super viewdidload];    NSBundle *bundle = [NSBundle mainbundle]; NSString *plistpath = [Bundle pathforresource:@ "Team_dictionary" oftype:@ "plist"        ];    Get all data in the attribute list file Self.dicdata = [[Nsdictionary alloc] initwithcontentsoffile:plistpath];    Nsarray *templist = [Self.dicdata AllKeys]; To sort key//selector to SEL type, the parameters of the Sortedarrayusingselector method must be selector self.listgroupname = [Templist Sortedarrayusi Ngselector: @selector (compare:)];}    -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} #pragma mark rewrite the data source Method-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{//According to the section cable    Derive group name from group an array group nsstring *groupname = [Self.listgroupname objectatindex:section];    Take the group name as key and remove the team array from the dictionary//Because the hash structure is stored as nsarray *listteams = [Self.dicdata objectforkey:groupname]; //How many teams return [Listteams Count] in this section;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{static    NSString *cellidentifier = @ "Cellidentifier";    UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier]; if (cell = = nil) {cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:celli    Dentifier];    }//Gets the selected section A group of 1th nsuinteger section = [indexpath sections];    Get the row index selected in the selection section--Group A 1th, 2nd ...    Nsuinteger row = [Indexpath row];    Get group name from group an array group by section index--Group A nsstring *groupname = [Self.listgroupname objectatindex:section];        Take the group name as key and remove the team array collection from the dictionary nsarray *listteams = [Self.dicdata objectforkey:groupname];    Cell.textLabel.text = [Listteams objectatindex:row]; return cell;} -(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{//The section number of rows return [Self.listgroupname count];} -(NSString *) TableView: (UITableView *) TablevieW titleforheaderinsection: (nsinteger) section{//section header of the section nsstring *groupname = [Self.listgroupname objectAtIndex:secti    On]; return groupName;} -(Nsarray *) Sectionindextitlesfortableview: (UITableView *) tableview{//Add index Nsmutablearray *listtitles = [[NSMutab    Learray Alloc] initwithcapacity:[self.listgroupname Count]];        Change Group A to (NSString *item in self.listgroupname) {nsstring *title = [item substringtoindex:1];//Get first character only  [Listtitles Addobject:title]; Append the last position of the string array} return listtitles;} @end

Attention:

In the story version to configure the TableView delegation AGREEMENT!!!!

Add index (iOS development)

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.