【问题标题】:How to display JSON array on UITableView如何在 UITableView 上显示 JSON 数组
【发布时间】:2012-06-07 06:48:30
【问题描述】:

JSON 数据是这样的......

{"order_list_1":[
{"name":"@SEVEN (7) OCLOCK BLADE PLATINUM","qty":1,"mrp":0.0},
{"name":"ACT 2 POPCORN BUTTER PEPPER","qty":2,"mrp":0.0},
{"name":"ACT 2 POPCORN CHILLY SURPRISE","qty":3,"mrp":0.0},
{"name":"@MAGGI SOUP HOT N SOUR(1 1)","qty":4,"mrp":0.0},
{"name":"ACT 2 POPCORN GOLDEN SIZZLE","qty":5,"mrp":0.0},
{"name":"AMCHUR AAKHA 1kg","qty":6,"mrp":0.0}]

}

json 数据显示在控制台而不是 tableview..

这是我的代码 sn-p...

-(void)fetchData{

dispatch_async(bKQueue, ^{
    NSData *data = [NSData dataWithContentsOfURL:latestURL];


    NSLog(@"LatestURL:%@",latestURL);       
    NSError* error=nil;  

    //Creating JSON Object
    NSDictionary *jsonDict= [NSJSONSerialization JSONObjectWithData:data                                                      options:kNilOptions error:&error];



    NSLog(@"JSON = %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

    //NSLog(@"dataaaa :%@",jsonDict);

    //[self performSelectorOnMainThread:@selector(fetchData:) withObject:data waitUntilDone:YES];



    dispatch_async(dispatch_get_main_queue(), ^{

     //[self fetchData:responseData];
     [self.tableView reloadData];
     //NSLog(@"Value :%@",data);
     });




    NSDictionary *statuses=[jsonDict objectForKey:@"order_list_1"];


    NSLog(@"SomeStatus :%@",statuses);

    if (!statuses)
     {
        NSLog(@"Error in Json :%@",error);
     }
    else
    {


        for(NSDictionary *newValu in statuses)
    {

            NSString *name=[newValu objectForKey:@"name"];

            NSString *qty=[newValu objectForKey:@"qty"];

            NSString *mrp=[newValu objectForKey:@"mrp"];

            NSLog(@"Name :%@    Quantity :%@    MRP :%@ ",name,qty,mrp);
        }
    }


});

}

这是我的 Tableview 代码..

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 静态 NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    
    
    
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    } 
    
    
    
    NSError *error=nil;         
    NSDictionary *jsonDict=[NSJSONSerialization JSONObjectWithData:responseData      options:kNilOptions error:&error];         
    NSArray *statuses=[jsonDict objectForKey:@"order_list_1"];              
    for (int i=0; i<jsonDict.count; i++) {        
        NSDictionary *newDict=[statuses objectAtIndex:i];                 
    NSLog(@"name :%@",[newDict valueForKey:@"name"]);    
    }
    NSDictionary *text=[statuses objectAtIndex:indexPath.row];    
    cell.textLabel.text=[text objectForKey:@"name"];    
    
    
    
    [self.tableView reloadData];
    return cell;
    

}

【问题讨论】:

  • NSLog(@"name :%@",[newDict valueForKey:@"name"]);这是什么输出?它打印在控制台上吗?
  • 1.正确格式化您的代码。 2.responseData是什么? 3. 为什么要在cellForRowAtIndexPath 中创建字典对象,为什么里面有for-loop? 4、为什么要从cellForRowAtIndexPath重新加载tableView

标签: json uitableview nsarray nsdictionary nsjsonserialization


【解决方案1】:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [[UITableViewCell alloc]init];

        NSArray *geomery = [res objectForKey:@"results"];
        NSDictionary *item = [geomery objectAtIndex:indexPath.row];
        NSString *name = [item objectForKey:@"name"];
        //NSLog(name);
        //cell.textLabel.text =@"test" ;
        cell.textLabel.text= name;



        return cell;
    }

这对我有用,检查字典索引是否正确发送

【讨论】:

    【解决方案2】:
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        }
        dictJson = [self.aryJson objectAtIndex:indexPath.row];
        cell.textLabel.text = [dictJson objectForKey:@"title"];
        cell.textLabel.text = [dictJson objectForKey:@""];
    
        return cell;
    
    }
    

    【讨论】:

      【解决方案3】:

      试试这个。

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
              static NSString *CellIdentifier = @"Cell";
      
              UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
              if (cell == nil) {
      
               cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
              } 
      
              NSError *error=nil;         
              NSDictionary *jsonDict=[NSJSONSerialization JSONObjectWithData:responseData      options:kNilOptions error:&error];         
              NSArray *statuses=[jsonDict objectForKey:@"order_list_1"];              
              NSDictionary *text=[statuses objectAtIndex:indexPath.row];    
              cell.textLabel.text=[text objectForKey:@"name"];    
              return cell;
      
      } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-01
        • 1970-01-01
        • 2016-02-13
        相关资源
        最近更新 更多