Effect:
The data model here has two layers: each group of cars is a model, and each row of cars is a model.
(1) We first create a wscars model.
In the WSCars.h:
#import <Foundation/Foundation.h> @interface wscars:nsobject@property (nonatomic,copy) NSString *icon;@ Property (nonatomic,copy) NSString *name;+ (Wscars *) Carswithdict: (Nsdictionary *) dict;-(Wscars *) Initwithdict: ( Nsdictionary *) dict; @end
In the WSCARS.M:
#import "WSCars.h" @implementation wscars+ (Wscars *) Carswithdict: (nsdictionary *) dict{ return [[Self alloc] Initwithdict:dict];} -(Wscars *) Initwithdict: (nsdictionary *) dict{ if ([Super init]) { [self setvaluesforkeyswithdictionary:dict] ; } return self;} @end
(2) Create a car group model again, Wscargroup.
In the WSCarGroup.h:
#import <Foundation/Foundation.h> @interface wscargroup:nsobject@property (nonatomic,copy) NSString * title;@ Property (Nonatomic,strong) Nsarray *cars;+ (Wscargroup *) Cargroupwithdict: (Nsdictionary *) dict;-(Wscargroup *) Initwithdict: (nsdictionary *) dict; @end
In wscargroup.m: (1 times a dictionary-to-model, which turns each car data into a Wscars object)
#import "WSCarGroup.h" #import "WSCars.h" @implementation wscargroup+ (Wscargroup *) Cargroupwithdict: (Nsdictionary *) dict{ return [[Self alloc]initwithdict:dict];} -(Wscargroup *) Initwithdict: (nsdictionary *) dict{ if ([Super init]) { self.title=dict[@ "title"]; Nsarray *dictarray=dict[@ "Cars"]; Nsmutablearray *muarray=[[nsmutablearray Alloc]init]; For (Nsdictionary * dic in Dictarray) { wscars *car=[[wscars alloc]initwithdict:dic]; [Muarray Addobject:car]; } Self.cars=muarray; } return self;} @end
(3) Then in VIEWCONTROLLER.M, define the array and turn the dictionary to the model
@property (Nonatomic,strong) Nsarray *carsarray;
Dictionary to model-(Nsarray *) carsarray{ if (_carsarray==nil) { nsstring *path=[[nsbundle mainbundle]pathforresource:@ "Cars_total.plist" Oftype:nil]; Nsarray *totalarray=[nsarray Arraywithcontentsoffile:path]; Nsmutablearray *muarray=[[nsmutablearray Alloc]init]; For (Nsdictionary *dict in Totalarray) { wscargroup *cargroup=[[wscargroup alloc]initwithdict:dict]; [Muarray Addobject:cargroup]; } _carsarray=muarray; } return _carsarray;}
The array work is done here.
(4) Drag a tableview and define the variables. This controller is treated as a data source and therefore complies with the Protocol.
@interface Viewcontroller () <UITableViewDataSource> @property (weak, nonatomic) Iboutlet UITableView *tableview;
(5) and set the data source to the current controller, by the way, set the line height
-(void) Viewdidload { //Set data source self.tableview.datasource=self; Set line height self.tableview.rowheight=60; [Super Viewdidload];}
(6) Set the group, row, and cell data and group names of the TableView.
Set how many groups-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return self.carsArray.count;} Set number of lines-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{Wscargroup * Cargroup=self.carsarray[section]; return carGroup.cars.count;} Set cell contents-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{// Cache pool First, performance optimization static nsstring *[email protected] "car"; UITableViewCell *cell=[self.tableview Dequeuereusablecellwithidentifier:id]; if (cell==nil) {Cell=[[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault ReuseIdentifier:ID]; }//Remove data wscargroup *cargroup=self.carsarray[indexpath.section]; Wscars *cars=cargroup.cars[indexpath.row]; Assigning values to cell cell.textlabel.text=cars.name; Cell.imageview.image=[uiimage ImageNamed:cars.icon]; return cell;} Set group name-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) section{WSCArgroup *cargroup=self.carsarray[section]; return cargroup.title;}
(7) Set up Group index
-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) tableview{ //need to return an array // Valueforkey can only be found in this level dictionary, and Self.carsarray is an array, and there is no title keyword //with Valueforkeypath to find in this and subordinate dictionary arrays, there is a path path return [Self.carsarray valueforkeypath:@ "title"];}
(8) for easy display viewing:
Hide Status bar-(BOOL) prefersstatusbarhidden{ return YES;}
Summarize:
-The difficulty lies in the dictionary-to-model place, because the model has 2 levels.
--Added a knowledge point that displays the group index. With the Sectionindextitlesfortableview method, the return value is an array, so here we also use the Valueforkeypath method to get a string group.
iOS Development-60 case Study: TableView settings for multiple sets of data, increased right group index, multi-tier data model settings, and Valueforkeypath