[UICollectionViewCell imageView]:无法识别的选择器发送到实例

Posted

技术标签:

【中文标题】[UICollectionViewCell imageView]:无法识别的选择器发送到实例【英文标题】:[UICollectionViewCell imageView]: unrecognized selector sent to instance 【发布时间】:2014-11-07 07:00:58 【问题描述】:

我正在尝试使用集合视图并在其中显示静态图像,但出现以下错误:

[UICollectionViewCell imageView]:发送到实例的无法识别的选择器。

我已配置单元格identifire = Cell

这是我的代码:

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

    static NSString *CellIdentifier = @"Cell";

    SSCollectionViewCell *cell = (SSCollectionViewCell *)[collectionView  dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    int imageNumber = indexPath.row % 10;

    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"Image%d.jpg",imageNumber]];

    return cell;

这里是sscollectionViewCell.h 代码

@interface SSCollectionViewCell : UICollectionViewCell

@property (weak) IBOutlet UIImageView *imageView;

@end

这里是SSColletionViewCell.m 代码

#import "SSCollectionViewCell.h"
@interface SSCollectionViewCell ()

@end


@implementation SSCollectionViewCell
@synthesize imageView;

- (id)initWithFrame:(CGRect)frame

    self = [super initWithFrame:frame];
    if (self) 
        // Initialization code
    
    return self;

@end

谁能指出我哪里出错了?

【问题讨论】:

您是否注册了用于该标识符的 XIB 文件?如果不是,那么问题可能是单元格为零。 你还没有放入SSColletionViewCell.m代码 如果你没有使用 storyboard 或 xib 来标记 IBOutlet,那么你应该放置一个强引用而不是弱引用。如果是,请确认插座是否正确连接到班级。 我正在使用情节提要。抱歉,关于 .m 代码。它被跳过了。我刚刚更新了上面的代码。 【参考方案1】:

改变

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

[self.collectionView registerClass:[SSCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

【讨论】:

【参考方案2】:

你在 ViewDidLoad 方法中错过了这个

[self.collectionView registerClass:[SSCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];

【讨论】:

【参考方案3】:

您的单元格仍然是 UICollectionCell 而不是您的自定义 SSCollectionViewCell,因为它不知道消息(无法识别的选择器)。

查看故事板,确保选择 collectionCell,并更改它的类(右侧菜单中的第三个选项卡),在 CustomClass 中输入你的 now。

【讨论】:

以上是关于[UICollectionViewCell imageView]:无法识别的选择器发送到实例的主要内容,如果未能解决你的问题,请参考以下文章

Swift 检测 UICollectionViewCell 是不是被拖到另一个 UICollectionViewCell 之上

在 UICollectionViewCell 中添加模糊

如何使用 ExoPlayer 的 IMA 扩展在特定点展示广告?

UICollectionViewCell与UIScrollView取消didSelectItemAtIndexPath

UICollectionViewCell 子类问题

重用 UICollectionViewCell