UICollectionViewCell无法识别的选择器发送到实例

Hur*_*rkS 4 objective-c ios uicollectionview uicollectionviewcell

我创建了一个看起来像这样的UICollectionViewCell

#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
Run Code Online (Sandbox Code Playgroud)

我从我的一个视图控制器调用它,我正在尝试设置一个UICollectionView ..这是我在调用这个类的地方

- (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;
}
Run Code Online (Sandbox Code Playgroud)

问题是,我收到此错误然后我的应用程序陷入堆中.

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionViewCell imageView]: unrecognized selector sent to instance 0x16dc1a90'
Run Code Online (Sandbox Code Playgroud)

更新

我创建了一个.nib文件调用aPhotoCell,我从中删除了UIView并添加了一个UIImageView.然后我将对象Custom Class更改为PhotoCell,现在我可以从PhotoCell看到UIImageView的IBOutlet,我已经把它连接起来了,但我仍然收到同样的错误.从下面的评论和答案中,这是我认为必须要解决的问题.

Rob*_*egg 5

几件事..

在你的PhotoCell班级 - 申报公共财产:

 @property (weak, nonatomic) IBOutlet UIImageView *imageView;
Run Code Online (Sandbox Code Playgroud)

然后在你的PhotoCell xib(我假设这是你从哪里创建它?)将它连接到IBOutLet你创建的.

我认为你在做错了cellForItemAtIndexPath- 因为你的错误显示一个实例UICollectionViewCell没有响应cell.imageView.image = imageForCollection;

确保您的单元格标识符与您在storyboard/xib文件中放置的单元格标识符相同.重写您的cellForItemAtIndexPath方法,如下所示:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

编辑

看起来你创建了一个子类,但UICollectionViewCell从未xib为它创建过一个文件.由于您没有使用故事板,请执行以下操作:

在Xcode中:

创建一个新文件 - User Interface从左下方选择" ",iOS'然后"empty"从显示它的选择中进行选择.

创建xib文件后 - 转到它并删除UIView它.然后从右侧的对象库中查找UICollectionView Cell并将其拖动到视图上,然后将其拖放到您的视图UIImageViewUICollectionView cell.

现在选择UICollectionViewCell刚刚创建并将其类设置为PhotoCell类.IBOutlets上面连接你的喜欢.确保也设置' Reusable Cell'属性.这里很重要.

然后在viewController加载你的UICollectionView- 添加到viewDidLoad

        [photoCollectionView registerNib:[UINib nibWithNibName:@"THE XIB FILE NAME YOU JUST CREATED eg: PhotoCell" bundle:nil] forCellWithReuseIdentifier:@"REUSE IDENTIFIER HERE"];
Run Code Online (Sandbox Code Playgroud)

这应该对你有帮助.