Ios 带Json的UITableViewController
我试图用Json响应填充tableview这是我从回调中读取的JsonIos 带Json的UITableViewController,ios,objective-c,iphone,Ios,Objective C,Iphone,我试图用Json响应填充tableview这是我从回调中读取的Json { "Categories": { "Beer": [ { "id": "3", "nombre": "Bud ligth", "precio": "10",
{
"Categories": {
"Beer": [
{
"id": "3",
"nombre": "Bud ligth",
"precio": "10",
"categoriaid": "3",
"url": "false"
}
],
"Whiskey": [
{
"id": "2",
"nombre": "Red label",
"precio": "100",
"categoriaid": "2",
"url": "false"
}
]
}
}
这是我的代码,但它破坏了应用程序。关于如何更改代码,使其在tableview中填充相应的部分和每个部分中的行,有什么想法吗
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:self.menuInfo options:NSJSONReadingMutableContainers error:nil];
NSDictionary *cat = [json objectForKey:@"Categories"];
for(NSString *categories in cat){
Categorias *categorias = [[Categorias alloc]initWithNombre:categories];
NSDictionary *listTagDict = [cat objectForKey:categories];
for (NSString *prod in listTagDict) {
NSArray *rows = [listTagDict objectForKey:prod];
for (NSDictionary *rowDict in rows) {
NSString* pID = [rowDict objectForKey:@"id"];
NSString* pNombre = [rowDict objectForKey:@"nombre"];
NSString* pPrecio = [rowDict objectForKey:@"precio"];
NSString* pUrl = [rowDict objectForKey:@"url"];
Productos* productos = [[Productos alloc]initWithNombre:pNombre productoID:pID precio:pPrecio iconUrl:pUrl];
[categorias addRow:productos];
}
}
}
这是我的两个对象分类.m部分
@implementation Productos
-(id)initWithNombre:(NSString *)name productoID:(NSString *)pId precio:(NSString*)prec iconUrl:(NSString*)url{
self = [super init];
if (self) {
self.nombre = name;
self.productoID = pId;
self.precio = prec;
self.iconUrl = url;
}
return self;
}
@end
@interface Categorias(){
NSMutableArray *rows;
}
@end
@implementation Categorias
-(id)initWithNombre:(NSString *)name{
self = [super init];
if (self) {
self.nombre = name;
}
return self;
}
-(void)addRow:(Productos *)row {
[rows addObject: row];
}
-(NSArray *)rowData {
return [NSArray arrayWithArray: rows];
}
@end
您以错误的方式解析json响应, 试试这个
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:self.menuInfo options:NSJSONReadingMutableContainers error:nil];
NSDictionary *cat = [json objectForKey:@"Categories"];
NSMutableArray *categoryArray = [NSMutableArray new];
for(NSString *key in [cat allKeys]){
Categorias *category = [[Categorias alloc]initWithNombre:key];
NSArray *listTagDict = [cat objectForKey:key];
for (NSDictionary *prod in listTagDict) {
NSString* pID = [prod objectForKey:@"id"];
NSString* pNombre = [prod objectForKey:@"nombre"];
NSString* pPrecio = [prod objectForKey:@"precio"];
NSString* pUrl = [prod objectForKey:@"url"];
Productos* productos = [[Productos alloc]initWithNombre:pNombre productoID:pID precio:pPrecio iconUrl:pUrl];
[category addRow:productos];
}
[categoryArray addObject:category];
}
使用categoryArray
填充tableview。
在这种情况下,categoryArray
count将是section count,每个section都包含带有每个类别的rowData
数组的行
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [categoryArray count];
}
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
Category *category = [categoryArray objectAtIndex:section];
return category.nombre;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
Category *category = [categoryArray objectAtIndex:section];
NSArray *rows = [category rowData];
return [rows count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; return cell;
Category *category = [categoryArray objectAtIndex:indexPath.section];
NSArray *rows = [category rowData];
Product *product = [rows objectAtIndex:indexPath.row];
//populate cell with product
}
崩溃时收到什么消息..在通过
JSONObjectWithData
解析之前是JSON,然后是NSDictionary
(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{return[[categoryArray objectAtIndex:section]objectForKey:@“nombre”];}这对我不起作用?!