将JSON元素放在TableView中作为Sections Title和Cell title

时间:2016-12-17 11:38:44

标签: ios objective-c json xcode

I need it like this

下面是JSON,需要将它放在表格视图中,以便" subitemname"成为章节标题和" subtosubitemname"成为细胞的称号。 我正在尝试,但没有成功。

部分标题已到位,但未检索行标题。

这是我试过的代码。

- (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;
}

其中subname是一个包含" subitemname"的数组。对象和_SubItemName是一个具有" subtosubitemname"的数组。对象。

{ "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"
}]}]}

2 个答案:

答案 0 :(得分:4)

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;
}

希望这对你有用。

答案 1 :(得分:1)

试试这个......只需一个数组即可。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;
}