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
- #import <UIKit/UIKit.h>
- @interface ikrboyviewcontroller5:uiviewcontroller{
- Nsmutabledictionary *dict;
- }
- @end
#import <UIKit/UIKit.h> @interface ikrboyviewcontroller5:uiviewcontroller{ nsmutabledictionary *dict;} @end
2. Add the following modifications to the CONTROLVIEW.M
CPP Code
- -(void) Viewdidload
- {
- [Super Viewdidload];
- [Self inittableviewdata];
- 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];
- }
- }
- }
-(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
- How many groups are divided
- -(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView
- {
- return [[Dict AllKeys] count];
- }
- Number of data units per grouping
- -(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 methods, do not display grouped headings
- -(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) Section
- {
- //dict AllKeys removed key arr No order, need to be sorted
- Nsarray *arr = [[Dict AllKeys] Sortedarrayusingselector: @selector (compare:)];
- return [arr objectatindex:section];
- }
- Index hint to the right of the list, do not implement the following method, do 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]; //When selected, the inverse color disappears instantly
- 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);
- }
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