We can parse the data as shown, use the value of "name" as the header of the partition header, and use the circled dictionary as a model, and we will resolve it in two ways:
The first way is to use an array and a dictionary, which is used to store the value of "name" as a key; the dictionary is used to store all the data;
The second way is to use two arrays, where one array is used to store the value of "name", and the other array stores all the data;
Gossip: Directly on the code
The first way:
#import "RootTableViewController.h"#import "YHCell.h"@interfaceRoottableviewcontroller () @property (nonatomic, strong) Nsmutablearray*Allkeysarray, @property (nonatomic, strong) Nsmutabledictionary*alldatadict;@end@implementationRoottableviewcontroller//Lazy Loading-(Nsmutabledictionary *) alldatadict {if(!_alldatadict) {_alldatadict=[Nsmutabledictionary dictionary]; } return_alldatadict;}-(Nsmutablearray *) Allkeysarray {if(!_allkeysarray) {_allkeysarray=[Nsmutablearray array]; } return_allkeysarray;}- (void) viewdidload {[Super viewdidload]; Self.navigationItem.title=@"Travel"; Self.tableView.showsVerticalScrollIndicator=NO; //Data Processing[self loadwebdata]; //Register Cell[Self.tableview Registerclass:[yhcellclass] Forcellreuseidentifier:@"Cell"]; }//Data Processing- (void) Loadwebdata {nsurl*url = [Nsurl urlwithstring:@"http://api.yhouse.com/m/city/dynmiclist"]; Nsurlrequest*request =[Nsurlrequest Requestwithurl:url]; Nsurlsession*session =[Nsurlsession sharedsession]; Nsurlsessiontask*task = [Session datataskwithrequest:request completionhandler:^ (NSData * _nullable data, Nsurlresponse * _Nullable RESPO NSE, Nserror *_nullable Error) { if(Error = =Nil) {Nsdictionary*dict =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil]; Nsdictionary*dict1 = dict[@"Data"]; Nsarray*array = dict1[@"allcity"]; for(inti =0; i < Array.count; i++) {nsdictionary*dict2 =Array[i]; Nsmutablearray*dataarray =[Nsmutablearray array]; for(NSString *keyinchdict2) { if([Key isequaltostring:@"name"]) {nsstring*value =Dict2[key]; [Self.allkeysarray Addobject:value]; } if([Key isequaltostring:@"Tabs"]) { for(Nsdictionary *dict3inchDict2[key]) {Model*model =[[Model alloc] init]; [Model SETVALUESFORKEYSWITHDICTIONARY:DICT3]; [DataArray Addobject:model]; }}} [Self.alldatadict Setobject:dataarray forkey:[self.allkeysarray las TObject]]; } [Self.tableview Reloaddata]; } }]; [Task resume];}//set Number of partitions-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {returnSelf.dataHandle.allKeysArray.count;}//set the number of rows per partition-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {nsstring*key =Self.datahandle.allkeysarray[section]; Nsinteger Index=[Self.datahandle.alldatadict[key] count]; returnindex;}//Return to Cell-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {Yhcell*cell = [TableView dequeuereusablecellwithidentifier:@"Cell"Forindexpath:indexpath]; Model*model =[[Model alloc] init]; NSString*key =Self.datahandle.allkeysarray[indexpath.section]; Model=Self.datahandle.alldatadict[key][indexpath.row]; //Assigning a value to a cell[Cell Bindmodel:model]; returncell;}//Set Cell height-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath {return -;}//Set Header header-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) Section {returnself.datahandle.allkeysarray[section];}@end
The first way of parsing data in the middle can also be fully used Foin loop to parse, that is, it can also simplify a bit of code:
- (void) Loadwebdata {nsurl*url = [Nsurl urlwithstring:@"http://api.yhouse.com/m/city/dynmiclist"]; Nsurlrequest*request =[Nsurlrequest Requestwithurl:url]; Nsurlsession*session =[Nsurlsession sharedsession]; Nsurlsessiontask*task = [Session datataskwithrequest:request completionhandler:^ (NSData * _nullable data, Nsurlresponse * _Nullable RESPO NSE, Nserror *_nullable Error) { if(Error = =Nil) {Nsdictionary*dict =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil]; Nsdictionary*dict1 = dict[@"Data"]; Nsarray*array = dict1[@"allcity"]; for(Nsdictionary *dict2inchArray) {[Self.allkeysarray addobject:dict2[@"name"]]; Nsmutablearray*array1 =[Nsmutablearray array]; for(Nsdictionary *dict3inchdict2[@"Tabs"]) {Model*model =[[Model alloc] init]; [Model SETVALUESFORKEYSWITHDICTIONARY:DICT3]; [Array1 Addobject:model]; } [Self.alldatadict setobject:array1 forkey:[self.allkeysarray Lastobject]; }} [Self.tableview Reloaddata]; }]; [Task resume];}
The second method:
#import "RootTableViewController.h"#import "YHCell.h"@interfaceRoottableviewcontroller () @property (nonatomic, strong) Nsmutablearray*Allkeysarray, @property (nonatomic, strong) Nsmutablearray*Alldataarray;@end@implementationRoottableviewcontroller//Lazy Loading-(Nsmutablearray *) Allkeysarray {if(!_allkeysarray) {_allkeysarray=[Nsmutablearray array]; } return_allkeysarray;}-(Nsmutablearray *) Alldataarray {if(!_alldataarray) {_alldataarray=[Nsmutablearray array]; } return_alldataarray;}- (void) viewdidload {[Super viewdidload]; Self.navigationItem.title=@"Travel"; Self.tableView.showsVerticalScrollIndicator=NO; //Data Processing[self loaddata]; //Register Cell[Self.tableview Registerclass:[yhcellclass] Forcellreuseidentifier:@"Cell"]; }//Data Processing- (void) LoadData {nsurl*url = [Nsurl urlwithstring:@"http://api.yhouse.com/m/city/dynmiclist"]; Nsurlsession*session =[Nsurlsession sharedsession]; Nsurlsessiontask*task = [Session Datataskwithurl:url completionhandler:^ (NSData * _nullable data, Nsurlresponse * _nullable response, NSEr ROR *_nullable Error) {Nsdictionary*dict =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil]; Nsdictionary*dict1 = dict[@"Data"]; Nsarray*array = dict1[@"allcity"]; for(Nsdictionary *dict2inchArray) {Nsmutablearray*array1 =[Nsmutablearray array]; [Self.allkeysarray addobject:dict2[@"name"]]; for(Nsdictionary *dict3inchdict2[@"Tabs"]) {Model*model =[[Model alloc] init]; [Model SETVALUESFORKEYSWITHDICTIONARY:DICT3]; [Array1 Addobject:model]; } [Self.alldataarray Addobject:array1]; } [Self.tableview Reloaddata]; }]; [Task resume];}//Number of partitions-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {returnSelf.allKeysArray.count;}//number of rows per partition-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {return[Self.alldataarray[section] count];}//Return to Cell-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {Yhcell*cell = [TableView dequeuereusablecellwithidentifier:@"Cell"Forindexpath:indexpath]; Model*model =[[Model alloc] init]; Nsarray*array =Self.alldataarray[indexpath.section]; Model=Array[indexpath.row]; //Assigning a value to a cell[Cell Bindmodel:model]; returncell;}//Set Cell height-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath {return -;}//Set Header header-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) Section {returnself.allkeysarray[section];}@end
Parse a slightly more complex piece of data