iosrestuicollectionview

Binding UICollectionView with rest API data issue


I am trying to grab images from external API and bind it to my UICollectionView & UIImageView cell with in that View. I am able to get the data and print it in the log file. However, I am not able to see the images on my UICollectionView. Here is the code to my data bindings.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   ImagesViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];    
  // imagesArray is an array with serialized json data.
  NSDictionary *finalImages = [self.imagesArray objectAtIndex:indexPath.row];    
  NSLog(@"Entering collection view.....");
  [[cell imageViewCell]setImage:[UIImage imageNamed:[finalImages valueForKey:@"link"]]];    
  return cell;
}

The is data is coming in JSON format:

 data
{
  abc: 'abc',
  xyz: 'xyx',
  link: 'link to an online image'
}

Solution

  • imageNamed is a method to get image from a image file in your bundle (image.png).

    In the case you want to get image from URL, download it as Mr Richhard Brown answer or use SDWebImage(set directly with imageView)

    Sample colectionView SDWebImage code(nonARC): https://github.com/lequysang/github_zip/blob/master/CollectionViewNonARC.zip