简体   繁体   English

UICollectionView崩溃?

[英]UICollectionView Crash?

I'm trying to use a UICollectionView in collaboration with a UICollectionViewCell to display thumbnails of images. 我正在尝试与UICollectionView一起使用UICollectionViewCell来显示图像的缩略图。 The UICollectionViewCell 's used in my app are custom (simplistic) subclasses: 我的应用程序中使用的UICollectionViewCell是自定义(简单)子类:

#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: UICollectionViewFile's Owner中,我使用:

- (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 首次显示视图时,该视图可以正常工作,但在将其从调用[self dismissViewControllerAnimated:YES completion:nil]的委托中[self dismissViewControllerAnimated:YES completion:nil] ,如果不崩溃,就无法再次显示该视图。

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. 如果使用,则使用XIB或storyBoardCell的UICollectionViewCell。 simple drag drop your imageThumb imageView to MemeCell.h. 简单地将您的imageThumb imageView拖放到MemeCell.h。 Delete your setter inside MemeCell.m. 在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. 如果是MemeCell no Xib。 please init and add imageThumb as subview of memeCell.contentView. 请初始化并将imageThumb添加为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. *编辑:如果仅存储thumbImage,不显示,只需在Cell.h中声明,无需重写setter。

property(strong,nonomatic) UIImage *thumbImage;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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