Ios 将JSON元素作为节标题和单元格标题放在TableView中
下面是JSON,需要将其放在表视图中,以便“subitemname”成为小节标题,“subsubitemname”成为单元格标题。 我正在尝试,但没有成功 节的标题已就位,但未检索行的标题 这是我试过的代码Ios 将JSON元素作为节标题和单元格标题放在TableView中,ios,objective-c,json,xcode,Ios,Objective C,Json,Xcode,下面是JSON,需要将其放在表视图中,以便“subitemname”成为小节标题,“subsubitemname”成为单元格标题。 我正在尝试,但没有成功 节的标题已就位,但未检索行的标题 这是我试过的代码 - (void)viewDidLoad { [super viewDidLoad]; NSString *urlString = [NSString stringWithFormat:@"URL"]; NSMutableURLRequest *request = [[NSMutable
- (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”对象的数组
希望这对你有用
希望这对你有用。试试这个。。。一个数组就足够了<代码>菜单=[jsonDict objectForKey:@“菜单”]代码>
- (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][@"subtosubitemname]";
return cell;
}
试试这个。。。一个数组就足够了<代码>菜单=[jsonDict objectForKey:@“菜单”]代码>
- (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][@"subtosubitemname]";
return cell;
}
显示将json响应转换为nsarray的代码在哪里?@himanshumuradiya我添加了code@Shikha第一件事首先。。。永远不要以大写字母开头变量名。。现在谈谈你的问题。。仅一个阵列就足够了,即<代码>菜单=[jsonDict objectForKey:@“菜单”]将此数组用于所有方法,如
numberOfSectionsInTableView
。它应该是[menu count]
类似于其他methods@EICaptainv2.0那么在CellforRowatinexpath中该怎么做?@Shikha如果你理解我在之前的评论中所说的话,那就简单了。。。只需使用菜单[indexPath.section][@“Submenu”][indexPath.row][@“subsubitemname”]
,您还需要更改其他tableview方法来实现这一点。显示您将json响应转换为nsarray的代码在哪里?@himanshumuradiya我添加了code@Shikha第一件事首先。。。永远不要以大写字母开头变量名。。现在谈谈你的问题。。仅一个阵列就足够了,即<代码>菜单=[jsonDict objectForKey:@“菜单”]将此数组用于所有方法,如numberOfSectionsInTableView
。它应该是[menu count]
类似于其他methods@EICaptainv2.0那么在CellforRowatinexpath中该怎么做?@Shikha如果你理解我在之前的评论中所说的话,那就简单了。。。只需使用菜单[indexath.section][@“Submenu”][indexath.row][@“subsubitemname”]
,您还需要更改其他tableview方法以实现这一点。这一行中的节出现错误NSDictionary*cellData=[[menuData objectAtIndex:section]objectForKey:@“Submenu”]objectAtIndex:indexath.row];put NSDictionary*cellData=[[menuData objectAtIndex:indexPath.section]objectForKey:@“子菜单”]objectAtIndex:indexPath.row];它在此行的节中给出错误NSDictionary*cellData=[[menuData objectAtIndex:section]objectForKey:@“子菜单”]objectAtIndex:indexPath.row];put NSDictionary*cellData=[[menuData objectAtIndex:indexPath.section]objectForKey:@“子菜单”]objectAtIndex:indexPath.row];
- (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][@"subtosubitemname]";
return cell;
}