I have a single view controller acting as both the dataSource
and delegate
for a UICollectionView
.
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
PhotoCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"PIC_CELL" forIndexPath:indexPath];
UIImage *photo = [UIImage imageWithData:[[_fetchedImages objectAtIndex:indexPath.row] valueForKey:@"picture"]];
if (photo) {
cell.photo = photo;
}
return cell;
}
In the above method I instantiate a custom collection view cell and attempt to set the photo
property of it.
PhotoCell.h
@interface PhotoCell : UICollectionViewCell {
}
@property (nonatomic, strong) IBOutlet UIImageView *imageView;
@property (nonatomic, strong) UIImage *photo;
@end
PhotoCell.m
@implementation PhotoCell
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
//customization
}
return self;
}
- (void)setPhoto:(UIImage *)photo {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(83.0, 83.0), NO, 0.0);
[photo drawInRect:CGRectMake(0, 0, 83, 83)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (_photo != newImage)
_photo = newImage;
self.imageView.image = _photo;
}
Here, I override the photo
property's setter, allowing for resizing of the passed photo before setting it as the property.
However, when the code executes and a PhotoCell
custom cell is instantiated, the following error is thrown when attempting to set the photo
property in the -cellForItemAtIndexPath:
method:
-[UICollectionViewCell setPhoto:]: unrecognized selector sent to instance 0x155c1270
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionViewCell setPhoto:]: unrecognized selector sent to instance 0x155c1270'
It seems that the system is perceiving the custom cell as an instance of its superclass, UICollectionViewCell
, rather than the actual class, PhotoCell
. The same error is thrown when not overriding the setter.
Why is the customized cell being seen as an instance of its superclass, causing the setter to be unrecognizable?