24

OK in my story board I have made a UICollectionView with 3 cells. One of the cells I made a custom class for that obviously extends the UICollectionViewCell:

enter image description here

And I registered the class in my ViewDiDApear:

[self.collectionView registerClass:[DeviceImageCell class] forCellWithReuseIdentifier:@"Cell1"];

Also I have added an imageView to that specific cell and an outlet for that imageView in my custom class. Problem occurs when I make my custom cell it forgets everything that was set in my storyboard, aka the background color, where my image view is and so on. Because of this my imageView returns nil all the time.

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    if(indexPath.row == 0)
    {
        static NSString *identifier = @"Cell1";
        DeviceImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        UIImage *image = [UIImage imageNamed:@"dysart-woods-full.jpg"];
        cell.imageview.image = image;
        cell.backgroundColor = [UIColor blueColor];

        //cell.imageview.image = image;
        return cell;
    }
    else if(indexPath.row == 1)
    {
        static NSString *identifier = @"Cell2";
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        return cell;
    }
    else
    {
        static NSString *identifier = @"Cell3";
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
        imageView.image = [UIImage imageNamed:@"dysart-woods-full.jpg"];

        return cell;
    }
}
LuisCien
  • 6,362
  • 4
  • 34
  • 42
  • 1
    Same problem here and no solution, yet. @Dmytro's answer didn't help unfortunately. I set text on a UILabel in the same custom cell and that works properly, but image does not appear in CollectionView. – race_carr Feb 22 '14 at 01:22
  • Did you ever resolve this? I am having the exact same issue in a new project. I have put image views in collection view cells previously with no issue... but for some reason, wether I create the cell in the storyboard or in a nib, the ImageView = nil yet the UILabels work fine. @#%)&(* – MobileVet May 01 '14 at 15:58

6 Answers6

53

You have probably long solved this issue.

However for those finding this and having the same issue

it is the registerClass call in your viewDidAppear

[self.collectionView registerClass:[DeviceImageCell class] forCellWithReuseIdentifier:@"Cell1"];

You are already registering the class in Interface Builder, so the call to registerClass:forCellWithReuseIdentifier: is replacing the entry created by IB.

Removing this line will fix the issue.

Chad Edrupt
  • 1,564
  • 11
  • 17
  • Omg I can't believe it was that simple. Then why the hell does Xcode add this line automatically when creating a UICollectionViewController? :( – Benoît Aug 25 '15 at 12:56
  • 2
    In case of nib you will have to register nib by using this: `[self.collectionView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellWithReuseIdentifier:@"CustomCellIdentifier"]; ` – atulkhatri Oct 13 '15 at 06:24
  • Thanks so much for this – Gary O' Donoghue Oct 06 '18 at 18:42
12

You should register the custom cell's nib if you are using one like this-

So it will be:

[self.collectionView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellWithReuseIdentifier:@"CustomCellIdentifier"];

instead of:

[self.collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:@"CustomCellIdentifier"];
atulkhatri
  • 10,896
  • 3
  • 53
  • 89
2

Well, this is odd. I know a solution, but I don't quite understand why it makes a difference. I had the same exact setup and the same problem. The IBOutlet was getting set to nil for the UIImageView, but not the UILabels.

Change your declaration for the UIImageView to be a strong property instead of an instance variable.

@property (strong) IBOutlet UIImageView* imageView;

This solved the issue for me. Clearly there is something with the way that UIImageView is connected / instantiated from the storyboard. As an added 'weird' factor, I use the same setup of ivars with a UITableViewCell and have no issue, only in the UICollectionViewCell

MobileVet
  • 2,958
  • 2
  • 26
  • 42
1

I had the similar problem and it's been solved by adding the following code in the DeviceImageCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        _imageview = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
        [self.contentView addSubview:_imageview];
    }
    return self;
}

Unfortunately I haven't managed to get _imageview initialised without this code addition.

Dmytro
  • 2,522
  • 5
  • 27
  • 36
0

Also check this one,

So now when you declare Custom CollectionView Cell in its Storyboard you need to specify the Attribute named "Identifier" under Collection Reusable View to some String. Now this may sound weired but , Check if name of Identifier is not same as Collection View Cell's class name. Class/File name of Cell and Cell Identifier must be Different.

That really worked for me , hence posting.

Vinod Supnekar
  • 143
  • 1
  • 7
-1

if you are using storyboards then remove below line.

collectionview.register(CollectionViewCell.self, forCellWithReuseIdentifier: "collectionViewCell")