简体   繁体   中英

UICollectionView Crash?

I'm trying to use a UICollectionView in collaboration with a UICollectionViewCell to display thumbnails of images. The UICollectionViewCell 's used in my app are custom (simplistic) subclasses:

#import "MemeCell.h"

@implementation MemeCell

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

}
return self;
}

-(void)setThumb:(UIImage *)image {
    if (_thumb != image) {
        _thumb = image;
    }

    _imageThumb.image = _thumb;
}

@end

And in the File's Owner of my UICollectionView , I use:

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

    static BOOL nibLoaded = NO;

if (!nibLoaded) {
    UINib *cellNib = [UINib nibWithNibName:@"MemeCell" bundle:nil];
    [_collectionView registerNib:cellNib forCellWithReuseIdentifier:@"MemeCell"];
    nibLoaded = YES;
}

MemeCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MemeCell" forIndexPath:indexPath];

NSString *path = [_imageThumbs objectAtIndex:indexPath.section];
UIImage *thumb = [UIImage imageNamed:path];
[cell setThumb:thumb];

return cell;
}

to return a cell. The view works fine when it is first presented, but after dismissing itself from it's delegate calling [self dismissViewControllerAnimated:YES completion:nil] , it cannot be presented again without crashing with a

2013-03-10 22:11:35.448 CapifyPro[21115:907] -[UICollectionViewCell setThumb:]: unrecognized selector sent to instance 0x1e593320
2013-03-10 22:11:35.450 CapifyPro[21115:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionViewCell setThumb:]: unrecognized selector sent to instance 0x1e593320'

Can anyone provide insight?

If use use UICollectionViewCell by XIB or storyBoardCell. simple drag drop your imageThumb imageView to MemeCell.h. Delete your setter inside MemeCell.m. Then set in dequence:

MemeCell *cell = (MemeCell*) [cv dequeueReusableCellWithReuseIdentifier:@"MemeCell" forIndexPath:indexPath];
cell.imageThumb.image = [UIImage imageNamed:path]

In the case MemeCell no Xib. please init and add imageThumb as subview of memeCell.contentView.

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
     self.imageThumb = [[UIImageView alloc] initWithFrame:self.frame];
     [self.contentView addSubviews:self.imageThumb];

}
return self;
}

*Edit: If you just store your thumbImage, don't display, just declare in Cell.h, don't need to rewrite setter.

property(strong,nonomatic) UIImage *thumbImage;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM