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

Ios UICollectionViewCell无法识别的选择器已发送到实例,ios,objective-c,uicollectionview,uicollectionviewcell,Ios,Objective C,Uicollectionview,Uicollectionviewcell,我创建了一个UICollectionViewCell,如下所示 #import "PhotoCell.h" @implementation PhotoCell @synthesize imageView; - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UICol

我创建了一个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
我正在试图设置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;
}
问题是,当我遇到这个错误时,我的应用程序就成了一堆

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICollectionViewCell imageView]: unrecognized selector sent to instance 0x16dc1a90'
更新 我创建了一个名为aPhotoCell的.nib文件,并从中删除了UIView,添加了一个UIImageView。然后我将objects自定义类更改为PhotoCell,现在我可以从PhotoCell看到UIImageView的IBOutlet,我已经连接了它,但仍然收到相同的错误。从下面的评论和答案来看,这是我认为我必须做的事情

  • 根据,,
    CollectionViewCell
    只是没有
    imageView
    属性。您应该首先将其添加到情节提要/代码中的单元格的
    contentView

  • 此外,您正在使用自己的
    光电管
    类。检查您的单元格在情节提要中是否具有正确的类名


  • 我假设由“cell”标识的单元格是在Xib文件中定义的。如果是这样,我怀疑错误是因为Xib文件中的主视图是用
    UICollectionViewCell
    类定义的,而不是自定义的
    PhotoCell

    编辑:
    在了解到您没有使用xib或故事板后,发生的情况是,
    dequeueReusableCellWithReuseIdentifier
    找不到任何具有给定标识符“cell”的单元格,因为您没有在任何地方定义它。从文件中:

    必须使用
    注册表nb:forCellReuseIdentifier:
    registerClass:forCellReuseIdentifier:
    方法,然后调用此 方法

    在您的情况下,您应该使用注册类:…用“cell”或任何其他标识符注册您的类。

    两件事

    在您的
    光电池
    类中-声明公共属性:

     @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    
    然后在您的
    光电管xib
    (我想这就是您创建它的地方?)中,将其连接到您创建的
    IBOutLet

    我认为您的
    cellForItemAtIndexPath
    有问题-因为您的错误显示
    UICollectionViewCell
    的实例没有响应
    cell.imageView.image=imageForCollection;
    

    确保您的单元标识符与您在序列图像板/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;
    }
    
    编辑

    似乎您创建了
    UICollectionViewCell
    的子类,但从未为其创建
    xib
    文件。由于您不使用故事板,请执行以下操作:

    在Xcode中:

    创建一个新文件-从左侧的“
    iOS”
    下选择“
    用户界面”
    ,然后从显示的选项中选择“empty”

    创建
    xib
    文件后,转到该文件并删除那里的
    UIView
    。然后从右侧的对象库中,查找
    UICollectionView单元格
    并将其拖到视图上,然后拖动
    UIImageView
    并将其放置在
    UICollectionView单元格中。

    现在选择刚刚创建的
    UICollectionViewCell
    并将其类设置为
    PhotoCell
    类。像上面那样连接您的
    IBOutlets
    。 确保还设置了“
    可重用单元”
    ”属性。这里很重要

    然后在加载
    UICollectionView的
    viewController
    中,将其添加到
    viewDidLoad

            [photoCollectionView registerNib:[UINib nibWithNibName:@"THE XIB FILE NAME YOU JUST CREATED eg: PhotoCell" bundle:nil] forCellWithReuseIdentifier:@"REUSE IDENTIFIER HERE"];
    

    这应该对你有帮助

    光电池xib文件需要在标识符中设置“电池” 您的代码应该是:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
    {
        PhotoCell *cell = (PhotoCell *)[self.photoCollectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        if (cell == nil) {
          // Load the top-level objects from the custom cell XIB.
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"cell" owner:self options:nil];
        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
        cell = [topLevelObjects objectAtIndex:0];
        }
        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;
    

    }我也有这个问题,我的问题是我的代码在另一个地方出错

    我使用此代码在viewDidLoad方法中注册UICollectionViewCell

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuseLabel"];
    
    但是,当我尝试使用自定义UICollectionViewCell更新单元格时,我没有更改此行以使用自定义单元格。因此,我将代码更改为:

    [self.collectionView registerClass:[MyCustomCell class] forCellWithReuseIdentifier:@"reuseLabel"];
    

    似乎委托方法-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath未返回光电元件对象我没有此类的xib文件,因为UICollectionViewCell的创建时没有xib。。他们是否允许你添加他们,所以我不知道该怎么办?我没有这个课程的故事板或笔尖。。当我创建它的时候,我没有选择用它制作一个。。我真的不知道该怎么做。谢谢。。问题是,我已经尝试创建一个xib,然后使用自定义类连接它并连接IBOutlet,但我仍然收到相同的错误。CHAMP!!!非常感谢!哈哈,我就快到了,没有使用UICollectionViewCell,而是添加了Imageview,我只是想用Imageview来做!这个工作非常好!!!真是太严重了!我的头一直撞在墙上!再次感谢!lolI现在创建了一个nib文件,将其customclass更改为PhotoCell,并连接了我制作的UIImageView。但我还是犯了同样的错误。。。我现在正在寻找更多的想法。