简体   繁体   中英

UITableViewController with Json

i'm trying to fill a tableview with a Json response this is the Json that i'm reading from the callback

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

and this is my code but it breaks the app any ideas on how can i make change my code in order to make it fill the tableview with its correspondent section and rows in each sections

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

here are my two object clases the .m part

@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

you are parsing the json response in wrong way, try this,

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

use categoryArray to populate tableview. in this, categoryArray count will be section count, and each section contains rows with rowData array of each category.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM