I have created a UICollectionViewCell that looks like this
#import "PhotoCell.h"
@implementation PhotoCell
@synthesize imageView;
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.backgroundColor = [UIColor clearColor];
imageView = [[UIImageView alloc] initWithFrame:(CGRect){.origin = CGPointMake(4.0f, 4.0f), .size=CGRectInset(frame, 4.0f, 4.0f).size}];
imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.contentView addSubview:imageView];
}
return self;
}
@end
I am calling this from one of my view controllers where I am trying to set up a UICollectionView.. this is where I am calling this class
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
PhotoCell *cell = (PhotoCell *)[self.photoCollectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
NSDictionary *currentPhotoDict = [imageArray objectAtIndex:indexPath.row];
UIImage *imageForCollection = [UIImage imageWithData:[currentPhotoDict objectForKey:@"DImage"]];
// but I have no idea where to pass the imag....
cell.imageView.image = imageForCollection;
return cell;
}
The issue is, I am getting this error then my app falls over in a heap.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionViewCell imageView]: unrecognized selector sent to instance 0x16dc1a90'
I have created a .nib file call aPhotoCell and I have deleted the UIView from it and added a UIImageView. I have then changed the objects Custom Class to PhotoCell and now that I can see the IBOutlet of UIImageView from PhotoCell I have hooked that up, but still I am receiving the same error. From the comments and answers below, this is what I thought I had to do to fix.
Couple of things..
In your PhotoCell
class - declare a public property:
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
Then in your PhotoCell xib
(I assume this is where you're creating it from?) connect it to the IBOutLet
you created.
I think you're doing something wrong in your cellForItemAtIndexPath
- as your error shows an instance of UICollectionViewCell
doesn't respond to cell.imageView.image = imageForCollection;
Make sure your cell identifier is also the same as you put it in your storyboard / xib file
Rewrite your cellForItemAtIndexPath
method like this:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;{
PhotoCell *cell = [ self.photoCollectionView dequeueReusableCellWithReuseIdentifier:@"CORRECT CELL IDENTIFIER HERE" forIndexPath:indexPath];
NSDictionary *currentPhotoDict = [imageArray objectAtIndex:indexPath.row];
UIImage *imageForCollection = [UIImage imageWithData:[currentPhotoDict objectForKey:@"DImage"]];
cell.imageView.image = imageForCollection;
return cell;
}
Edit
It seems like you created a subclass of UICollectionViewCell
and never created a xib
file for it. Since you're not using storyboards do this:
In Xcode:
Create a new file - select "User Interface
" from the left under 'iOS'
and select "empty"
from the selection it shows you.
After creating your xib
file - go to it and delete UIView
that is there. Then from the object library on your right, look for UICollectionView Cell
and drag it onto the view and then drag a UIImageView
and place it in your UICollectionView cell.
Now select UICollectionViewCell
you just created and set its class to the PhotoCell
class. Connect your IBOutlets
like above.
Make sure to set the 'Reusable Cell
' attribute too. Its important here.
Then in the viewController
that loads your UICollectionView
- add this to viewDidLoad
[photoCollectionView registerNib:[UINib nibWithNibName:@"THE XIB FILE NAME YOU JUST CREATED eg: PhotoCell" bundle:nil] forCellWithReuseIdentifier:@"REUSE IDENTIFIER HERE"];
This should help you.
I also had this problem and my issue was that I had an error in my code in another place.
I used this code to register the the UICollectionViewCell in the viewDidLoad method.
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuseLabel"];
However, when I tried to update the cell with my custom UICollectionViewCell I didn't change this line to work my custom cell. So I changed the code to this:
[self.collectionView registerClass:[MyCustomCell class] forCellWithReuseIdentifier:@"reuseLabel"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With