2016-12-17 31 views
0

I need it like this穿戴JSON元素作为第标题和细胞标题

下面是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; 
} 

其中子名称是具有阵列“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" 
}]}]} 
+0

哪里是你的代码表明您转换您的JSON响应的NSArray? –

+0

@HimanshuMoradiya我添加了代码 –

+0

@Shikha第一件事...从不用大写字母开始变量名..现在为你的问题..只有一个数组就足够了即ie ..'menu = [jsonDict objectForKey:@“Menu “];'为'numberOfSectionsInTableView'之类的所有方法使用此数组。它应该是'[菜单计数]'像明智的其他方法 –

回答

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

希望这对你的工作。

+0

它NSDictionary * cellData = [[[menuData objectAtIndex:section] objectForKey:@“Submenu”] objectAtIndex:indexPath.row]; –

+0

put NSDictionary * cellData = [[[menuData objectAtIndex:indexPath.section] objectForKey:@“Submenu”] objectAtIndex:indexPath.row]; –