title: Showing image from imageURL in UICollectionView using server response
body: <p>What I am trying to do is render image in a <code>UICollectionView</code> from <code>imageUrl</code> which is coming from server as response. There is no issue with <code>UICollectionView</code> as I have already tested it with <code>hardcoded URL</code>. The problem starts when I try to load it with the <code>array</code> which consist <code>url</code> as key-value pair. May be, I don't know the exact position from where I have to call the <code>method which is responsible for getting the response from the server</code>. I am getting the response exactly as it should be and pass it into an array. While debugging I can see that <code>after getting response control is not going back to execute the</code>UICollectionView delegate methods<code>to render</code>imageUrl`. Here is the code, which I am trying.</p>
  
  <pre><code>#import "ProductCollectionViewController.h"
  #import "ProductCell.h"
  #import "UIImageView+WebCache.h"
  
  @interface ProductCollectionViewController ()
  {
    NSMutableData *receivedData;
    NSMutableArray *productList;
  
  }
  @end
  @implementation ProductCollectionViewController
  
  -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
      // Custom initialization
    }
    return self;
  }
  static NSString * const reuseIdentifier = @"Cell";
  -(void)viewDidLoad 
  {
    [super viewDidLoad];
    [self getProductList];
  }
  
  #pragma mark &lt;UICollectionViewDataSource&gt;
  
  -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
      return 1;
   }
  
   -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
  {
  
    return productList.count;
  }
  
  - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
  {
  
    ProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
  
    NSURL *url = [[productList objectAtIndex:indexPath.row]valueForKey:@"url"];
  
    [cell.productImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
  
    return cell;
  }
  
  -(void)getProductList
  {
    NSURLSessionConfiguration *defaultConfigObject =    [NSURLSessionConfiguration defaultSessionConfiguration];
  
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
  
    NSURL * url = [NSURL URLWithString:@“http:xxxxxxxxx"];
    NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];  
  
    NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithRequest:urlRequest];
  
    [dataTask resume];
  }
  
  -(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
  {
  
    NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
    NSLog(@"status code %li", httpResp.statusCode);
  
    receivedData=nil;
    receivedData=[[NSMutableData alloc] init];
    [receivedData setLength:0];
    completionHandler(NSURLSessionResponseAllow);
  }
  
  -(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
  {
    [receivedData appendData:data];
  }
  
   -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
  {
    if (error) {
      // Handle error
    }
    else {
       NSError *tempError;
  
       NSDictionary* response=(NSDictionary*)[NSJSONSerialization      JSONObjectWithData:receivedData options:kNilOptions error:&amp;tempError];
  
       NSLog(@"Response is :%@", response);
       productList = [NSMutableArray new];
       NSArray *rsBody = response [@"rsBody"];
       for(NSDictionary *dict in rsBody)       
       {
          [productList addObject:@{@“url":dict[@"productImageUrl"]}]
       }
  
      }
  
  }
  </code></pre>
  
  <p><strong>Response</strong>
  <code>
      {"rsBody":
       [{"productId":11,
      "productImageUrl":"http:xxxx"},
      {"productId":9,
      "productImageUrl":"http:"xxxx"}]}</code></p>