问题描述:

Below is JSON, and need to put it in table view such that "subitemname" becomes the section title and "subtosubitemname" becomes the title for cell.

I am trying it but didn't get successful.

Title for section is in place but title for rows is not retrieved.

here is the code I tried.

- (void)viewDidLoad {

[super viewDidLoad];

NSString *urlString = [NSString stringWithFormat:@"URL"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

[request setURL:[NSURL URLWithString:urlString]];

[request setHTTPMethod:@"GET"];

NSError *error;

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSString *str=[[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];

NSArray *DATAA=[jsonDict objectForKey:@"Menu"];

subid=[DATAA valueForKey:@"subitemid"];

subname=[DATAA valueForKey:@"subitemname"];

NSArray *sub= [DATAA valueForKey:@"Submenu"];

_SubItemName=[sub valueForKey:@"subtosubitemname"];

_SubItemId=[sub valueForKey:@"subtosubitemid"];

}

#pragma mark - Table view data source

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{ return [subname objectAtIndex:section];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return [subid count];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [_SubItemId count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

cell=[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

NSArray *subn=[_SubItemName objectAtIndex:indexPath.section];

cell.textLabel.text=[subn objectAtIndex:indexPath.row];

return cell;

}

where subname is an array having "subitemname" objects and _SubItemName is an array having "subtosubitemname" objects.

{ "Menu":

[{"subitemid":1,

"itemid":1,

"subitemname":"TELEVISIONS",

"ordernum":1,

"Submenu":

[{

"subitemid":1,

"subtosubitemid":1,

"itemid":1,

"subtosubitemname":"FULL HD TV"},

{

"subitemid":1,

"subtosubitemid":2,

"itemid":1,

"subtosubitemname":"SMART TV"

}

]

},

{

"subitemid":2,

"itemid":1,

"subitemname":"AUDIO & VIDEO",

"ordernum":2,

"Submenu":

[{

"subitemid":2,

"subtosubitemid":3,

"itemid":1,

"subtosubitemname":"HOME AUDIO SYSTEMS"

},

{

"subitemid":2,

"subtosubitemid":4,

"itemid":1,

"subtosubitemname":"DTH SERVICES"

},

{

"subitemid":2,

"subtosubitemid":5,

"itemid":1,

"subtosubitemname":"AUDIO & VIDEO ACCESSORIES"

},

{

"subitemid":2,

"subtosubitemid":11,

"itemid":1,

"subtosubitemname":"PROJECTORS"

}

]

},

{

"subitemid":3,

"itemid":1,

"subitemname":"LARGE APPLIANCES",

"ordernum":3,

"Submenu":

[

{

"subitemid":3,

"subtosubitemid":14,

"itemid":1,

"subtosubitemname":"WASHING MACHINES & DRYERS"

},

{

"subitemid":3,

"subtosubitemid":16,

"itemid":1,

"subtosubitemname":"AIR CONDITIONERS"

},

{

"subitemid":3,

"subtosubitemid":18,

"itemid":1,

"subtosubitemname":"REFRIGERATORS"

},

{

"subitemid":3,

"subtosubitemid":19,

"itemid":1,

"subtosubitemname":"INVERTERS & BATTERIES"

}]}]}

网友答案:

Try this ... Just one array is enough.. menu = [jsonDict objectForKey:@"Menu"];

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return menu[indexPath.section][@"subitemname"];    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     return [menu count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     NSArray *submenu = menu[indexPath.section][@"Submenu"];
     return [submenu count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    cell=[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    NSArray *submenu = menu[indexPath.section][@"Submenu"];
    cell.textLabel.text = submenu[indexPath.row][@"subtosu‌​bitemname]";
    return cell;
}
网友答案:
NSMutableArray *menuData = [jsonDict objectForKey:@"Menu"];

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
        return [[menuData objectAtIndex:section] objectForKey:@"subitemname"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
       return [menuData count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        NSArray *subMenuData = [[menuData objectAtIndex:section] objectForKey:@"Submenu"];
        return [subMenuData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       cell=[tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
       NSDictionary *cellData = [[[menuData objectAtIndex:section] objectForKey:@"Submenu"] objectAtIndex:indexPath.row];
      cell.textLabel.text=[cellData objectForKey:@"subtosubitemname"];
      return cell;
}

Hope this work for you.

相关阅读:
Top