-1

JSON data is something like that...

{"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 data is displaying on console but not on tableview..

Here my code snippet...

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


});

}

And this is my Tableview code..

  • (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"];              
    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;
    

}

iDev
  • 19
  • 7
  • NSLog(@"name :%@",[newDict valueForKey:@"name"]); what is the output of this? is it printed on console? – iMeMyself Oct 16 '12 at 11:37
  • 1. Format your code properly. 2. what is `responseData`? 3. Why are you creating dictionary object in `cellForRowAtIndexPath` and why there is `for-loop` in it? 4. Why are you reloading tableView from `cellForRowAtIndexPath` – Martin Dec 21 '17 at 11:11

3 Answers3

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

this worked for me , check out for dictionary index properly sent or not

adamdehaven
  • 5,890
  • 10
  • 61
  • 84
iMeMyself
  • 1,649
  • 13
  • 28
0
-(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;

}
László Papp
  • 51,870
  • 39
  • 111
  • 135
VarmaS
  • 1
  • 1
0

Try this.

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

} 
Martin
  • 846
  • 1
  • 9
  • 23