繁体   English   中英

带有Json的UITableViewController

[英]UITableViewController with Json

我正在尝试用Json响应填充表格视图,这是我从回调中读取的Json

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

这是我的代码,但是它破坏了应用程序有关如何更改我的代码的任何想法,以便使它的对应部分以及每个部分中的行填充到表格视图中

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计数将是部分计数,并且每个部分都包含具有每个类别的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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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