iOS Development Table View Add index

Source: Internet
Author: User

The effect we have to achieve is as follows.

1. Modify ControlView.h, that is, add variable dict, which is used to store the Tabelview data source.

CPP Code
    1. #import <UIKit/UIKit.h>
    2. @interface ikrboyviewcontroller5:uiviewcontroller{
    3. Nsmutabledictionary *dict;
    4. }
    5. @end
#import <UIKit/UIKit.h> @interface ikrboyviewcontroller5:uiviewcontroller{    nsmutabledictionary *dict;} @end

2. Add the following modifications to the CONTROLVIEW.M

CPP Code
  1. -(void) Viewdidload
  2. {
  3. [Super Viewdidload];
  4. [Self inittableviewdata];
  5. additional setup after loading the view.
  6. }
  7. -(void) inittableviewdata{
  8. NSBundle *bundle = [NSBundle mainbundle];
  9. NSString *plistpath = [bundle pathforresource:@"User_head" oftype:@ "plist"];
  10. Nsarray *dataarr = [[Nsarray alloc] initwithcontentsoffile:plistpath];
  11. //Divide all data into three groups
  12. Nsmutablearray *arr1 = [Nsmutablearray array];
  13. Nsmutablearray *ARR2 = [Nsmutablearray array];
  14. Nsmutablearray *ARR3 = [Nsmutablearray array];
  15. Dict = [Nsmutabledictionary dictionary];
  16. [Dict setobject:arr1 forkey:@"Group1"];
  17. [Dict setobject:arr2 forkey:@"Group2"];
  18. [Dict setobject:arr3 forkey:@"Group3"];
  19. //Set the basis for dividing the data, that is, according to index divided into three groups, index 0-1 for the first group, 2-4 for the second group, 5 for the third group
  20. For (Nsinteger index = 0; index < [Dataarr count]; index++) {
  21. Nsdictionary *item = [Dataarr objectatindex:index];
  22. if (index<2) {
  23. [Arr1 Addobject:item];
  24. }
  25. Else if (index>=2&&index<5) {
  26. [Arr2 Addobject:item];
  27. }
  28. Else if (index>=5) {
  29. [Arr3 Addobject:item];
  30. }
  31. }
  32. }
-(void) viewdidload{[Super Viewdidload]; [Self inittableviewdata];//does any additional setup after loading the view.}    -(void) inittableviewdata{nsbundle *bundle = [NSBundle mainbundle];    NSString *plistpath = [Bundle pathforresource:@ "User_head" oftype:@ "plist"];    Nsarray *dataarr = [[Nsarray alloc] initwithcontentsoffile:plistpath];    Divide all data into three groups nsmutablearray *arr1 = [Nsmutablearray array];    Nsmutablearray *ARR2 = [Nsmutablearray array];        Nsmutablearray *ARR3 = [Nsmutablearray array];    Dict = [Nsmutabledictionary dictionary];    [Dict setobject:arr1 forkey:@ "Group1"];    [Dict setobject:arr2 forkey:@ "Group2"];        [Dict setobject:arr3 forkey:@ "Group3"];        Set the basis for dividing the data, that is, according to index divided into three groups, index 0-1 for the first group, 2-4 for the second group, 5 for the third group for (Nsinteger index = 0; index < [Dataarr count]; index++) {        Nsdictionary *item = [Dataarr objectatindex:index];        if (index<2) {[arr1 addobject:item];          } else if (index>=2&&index<5) {  [Arr2 Addobject:item];        } else if (index>=5) {[Arr3 addobject:item]; }    }}

3. Initialize TableView

CPP Code
  1. How many groups are divided
  2. -(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView
  3. {
  4. return [[Dict AllKeys] count];
  5. }
  6. Number of data units per grouping
  7. -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
  8. {
  9. switch (section)
  10. {
  11. Case 0:
  12. {
  13. return [[Dict objectforkey:@"Group1"] count];
  14. }
  15. Case 1:
  16. {
  17. return [[Dict objectforkey:@"Group2"] count];
  18. }
  19. Case 2:
  20. {
  21. return [[Dict objectforkey:@"Group3"] count];
  22. }
  23. }
  24. return 0;
  25. }
  26. Grouped headings, do not implement the following methods, do not display grouped headings
  27. -(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) Section
  28. {
  29. //dict AllKeys removed key arr No order, need to be sorted
  30. Nsarray *arr = [[Dict AllKeys] Sortedarrayusingselector: @selector (compare:)];
  31. return [arr objectatindex:section];
  32. }
  33. Index hint to the right of the list, do not implement the following method, do not display the right index
  34. -(Nsarray *) Sectionindextitlesfortableview: (UITableView *) TableView
  35. {
  36. //dict AllKeys removed key arr No order, need to be sorted
  37. Nsarray *arr = [[Dict AllKeys] Sortedarrayusingselector: @selector (compare:)];
  38. return arr;
  39. }
  40. -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
  41. {
  42. static NSString *cellidentifier = @"Mytablecell";
  43. UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];
  44. Nsuinteger row = [Indexpath row];
  45. Nsuinteger section = [Indexpath section];
  46. Nsarray *arr;
  47. switch (section) {
  48. Case 0:
  49. arr = [Dict objectforkey:@"Group1"];
  50. Break ;
  51. Case 1:
  52. arr = [Dict objectforkey:@"Group2"];
  53. Break ;
  54. Case 2:
  55. arr = [Dict objectforkey:@"Group3"];
  56. Break ;
  57. Default:
  58. Break ;
  59. }
  60. Nsdictionary *rowdict = [arr objectatindex:row];
  61. Cell.textLabel.text = [rowdict objectforkey:@"ItemName"];
  62. NSLog (@"Cell.label.text =%@", [rowdict objectforkey:@"ItemName"]);
  63. NSString *imagepath = [rowdict objectforkey:@"Itemimagepath"];
  64. Cell.imageView.image = [UIImage Imagenamed:imagepath];
  65. NSLog (@"cell.image.image =%@", ImagePath);
  66. Cell.accessorytype = Uitableviewcellaccessorydisclosureindicator;
  67. return cell;
  68. }
  69. Select cell Response Event
  70. -(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{
  71. [TableView Deselectrowatindexpath:indexpath Animated:yes]; //When selected, the inverse color disappears instantly
  72. Nsuinteger row = [Indexpath row];
  73. Nsuinteger section = [Indexpath section];
  74. Nsarray *arr;
  75. switch (section) {
  76. Case 0:
  77. arr = [Dict objectforkey:@"Group1"];
  78. Break ;
  79. Case 1:
  80. arr = [Dict objectforkey:@"Group2"];
  81. Break ;
  82. Case 2:
  83. arr = [Dict objectforkey:@"Group3"];
  84. Break ;
  85. Default:
  86. Break ;
  87. }
  88. Nsdictionary *rowdict = [arr objectatindex:row];
  89. NSString *username = [rowdict objectforkey:@"ItemName"];
  90. NSLog (@"username=%@", userName);
  91. }
Divided into how many groups-(Nsinteger) Numberofsectionsintableview: (UITableView *) Tableview{return [[Dict AllKeys] count];} Number of data units per packet-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{switch (        section) {case 0: {return [[dict objectforkey:@] Group1 "] count];        } Case 1: {return [[Dict objectforkey:@ "Group2"] count];        } Case 2: {return [[Dict objectforkey:@ "Group3"] count]; }} return 0;} Grouped headings, do not implement the following method, do not display group headings-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (Nsinteger) section{//dict AllKeys takes out the key arr is no order and needs to be sorted nsarray *arr = [[Dict AllKeys] Sortedarrayusingselector: @selector (compare :)];return [arr objectatindex:section];} The index hint to the right of the list, does not implement the following method, does not display the right index-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) tableview{//dict AllKeys removed key arr No order, need to be sorted nsarray *arr = [[Dict AllKeys] sortedarrayusingselector: @selector (comPare:)]; return arr;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{static    NSString *cellidentifier = @ "Mytablecell";        UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];    Nsuinteger row = [Indexpath row];    Nsuinteger section = [Indexpath section];        Nsarray *arr;            Switch (section) {Case 0:arr = [dict objectforkey:@ "Group1"];        Break            Case 1:arr = [dict objectforkey:@ "Group2"];        Break            Case 2:arr = [dict objectforkey:@ "Group3"];        Break    Default:break;    } nsdictionary *rowdict = [arr objectatindex:row];    Cell.textLabel.text = [rowdict objectforkey:@ "ItemName"];        NSLog (@ "Cell.label.text =%@", [rowdict objectforkey:@ "ItemName"]);    NSString *imagepath = [rowdict objectforkey:@ "Itemimagepath"];   Cell.imageView.image = [UIImage Imagenamed:imagepath]; NSLog (@ "cell.image.image =%@", ImagePath);        Cell.accessorytype = Uitableviewcellaccessorydisclosureindicator; return cell;} Select cell Response Event-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{[TableView    Deselectrowatindexpath:indexpath animated:yes];//the selected inverse color disappears immediately nsuinteger row = [Indexpath row];    Nsuinteger section = [Indexpath section];        Nsarray *arr;            Switch (section) {Case 0:arr = [dict objectforkey:@ "Group1"];        Break            Case 1:arr = [dict objectforkey:@ "Group2"];        Break            Case 2:arr = [dict objectforkey:@ "Group3"];        Break    Default:break;    } nsdictionary *rowdict = [arr objectatindex:row];    NSString *username = [rowdict objectforkey:@ "ItemName"]; NSLog (@ "username=%@", UserName);}

    • Uitableview.zip (22.2 KB)

iOS Development Table View Add index

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.