How to create a custom UICollectionViewCell and add image from URL to it?

ios, objective-c, uicollectionview, uicollectionviewcell, uiimage

Solution

As I get it you are using a custom UICollectionViewCell. However in the method you pasted you create a pointer to UICollectionViewCell which has no built-in UIImageView. So that's why you get the error in the first try. About the second - did you register your custom UICollectionViewCell class with the CollectionView?

======

Edit: after a long chat with the poster we solved all the problems.

- The first one was that the registered class was UICollectionViewCell, not the inherited custom one.

- The second problem was that the view was actually in a xib file, so `registerNib:forCellWithReuseIdentifier:` should be used instead of `registerClass:forCellWithReuseIdentifier:`

- The third one was that the custom cell was configured in a storyboard xib instead of its own xib file that is wired with the custom class ("Cell" in our case)

- And fourth main problem was that UIImage was incorrectly created with `imageNamed:` which should be used only for images that are embedded in the project. The correct way to create the UIImage in our case is: `flickerCell.imageView.image = [UIImage imageNamed:[photoURLs objectAtIndex:indexPath.row]];`

Problem

i want to load my images i've got from flickr into uicollectionsview i followed allot of guides but they all fall apart when i have to do this ``` cell.imageView.image ``` it basically says it can't find imageView in object of uicollectionsviewcell i also tried a method like this, but it didn't work ``` - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identifier = @"flickerCell"; UICollectionViewCell *flickerCell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; flickerCell.backgroundColor = [UIColor whiteColor]; UIImageView *recipeImageView = (UIImageView *)[flickerCell viewWithTag:100]; recipeImageView.image = [UIImage imageNamed:[photoURLs objectAtIndex:indexPath.row]]; [flickerCell addSubview:recipeImageView]; return flickerCell; } ``` recipieImageView is something i got some a tutorial. my cell is named flickerCell in the storyboard and photoURLs has 33 objects i can view in code . so it's all there. also photoURLs is a NSMutablearray how ever. in this method my recipeImageView returns nill ??? i have a cell.h which has the imageView ``` #import <UIKit/UIKit.h> @interface Cell : UICollectionViewCell @property (strong, nonatomic) IBOutlet UIImageView *imageView; @end ``` so if anyone know a way i can display all the images that photoURLs has in a uicollectionview it would be very helpful UPDATE i now tried a new way but still not working. imageView returns nil ``` - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identifier = @"flickerCell"; Cell *flickerCell = (Cell*) [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; flickerCell.backgroundColor = [UIColor whiteColor]; imageView.image = [UIImage imageNamed:[photoURLs objectAtIndex:indexPath.row]]; return flickerCell; } ``` UPDATE AGAIN ``` - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identifier = @"flickerCell"; Cell *flickerCell = (Cell*) [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; NSLog(@"flickercell = %@", flickerCell); flickerCell.backgroundColor = [UIColor whiteColor]; flickerCell.imageView.image = [UIImage imageNamed:[photoURLs objectAtIndex:indexPath.row]]; return flickerCell; ``` } with this in viewdidload ``` [self.collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"flickerCell"]; ``` NSLOG of flickerCell returns this : ``` flickercell = <Cell: 0x9dec2f0; baseClass = UICollectionViewCell; frame = (0 0; 120 115); layer = <CALayer: 0x9dec430>> ```

Original source