0

I ma doing program in WebService... In main viewController....

AppListTableViewController *appList = [self.storyboard instantiateViewControllerWithIdentifier:@"ALTVC"];

self.dataTask = [self.urlSession dataTaskWithRequest:self.urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

     NSMutableDictionary *serverRes = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

     NSString *imageLink = [[[[[[serverRes objectForKey:@"feed"] objectForKey:@"entry"] objectAtIndex:0] objectForKey:@"im:image"] objectAtIndex:0] objectForKey:@"label"];

     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
     [defaults setObject:imageLink forKey:@"image"];

     [self.dataTask resume];
}

    [self.navigationController pushViewController:appList animated:YES];

From server image link came successfully... but how to send and display that image in tableView cell.

In tableView controller ....

cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[defaults objectForKey:@"image"]]]]; 

It's not display any image in cell...

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Marking
  • 184
  • 1
  • 2
  • 16

1 Answers1

1

the reason you were pushed your VC before completion on call, do like

self.dataTask = [self.urlSession dataTaskWithRequest:self.urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)     {

     NSMutableDictionary *serverRes = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

     NSString *imageLink = [[[[[[serverRes objectForKey:@"feed"] objectForKey:@"entry"] objectAtIndex:0] objectForKey:@"im:image"] objectAtIndex:0] objectForKey:@"label"];

   if (imageLink)
    {
      NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
     [defaults setObject:imageLink forKey:@"image"];
        // call your navigation inside the block
        AppListTableViewController *appList = [self.storyboard instantiateViewControllerWithIdentifier:@"ALTVC"];
       [self.navigationController pushViewController:appList animated:YES];
    }


    }];

     [self.dataTask resume];
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • for asynchronous loading the image in tableview cell , see this [link](http://stackoverflow.com/questions/16663618/async-image-loading-from-url-inside-a-uitableview-cell-image-changes-to-wrong) – Anbu.Karthik Feb 20 '17 at 07:44