【问题标题】:Storyboard, UICollectionview, unrecognized selector sent to instance故事板、UICollectionview、发送到实例的无法识别的选择器
【发布时间】:2013-06-26 16:22:08
【问题描述】:

我有一个承载 UICollectionview 的情节提要,在 collectionviewcontroller 中我实现了

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
              cellForItemAtIndexPath:(NSIndexPath *)indexPath
 {
   itemViewCell *itemCell =
   [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier
                                         forIndexPath:indexPath];

   return itemCell;
 }

但在 dequeueReusableCellWithReuseIdentifier 上,应用程序会抛出此异常:

 [__NSCFConstantString initWithFrame:]: unrecognized selector sent to instance 0x7443c60

所以我的第一个倾向是查看我正在创建的自定义单元格,以确保我有该方法的实现,并且我确实这样做了

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       self.backgroundColor = [UIColor whiteColor];
    }
    return self;
} 

当然,我正在 viewdidload 中注册该单元格

 [super viewDidLoad];   
 [self.collectionView registerClass:[CellIdentifier class]
       forCellWithReuseIdentifier:CellIdentifier];

但我不认为这是相关的,因为我在 initWithFrame 方法上放了一个断点,它永远不会热它。我做错了什么。

【问题讨论】:

    标签: ios objective-c ios6 uicollectionview


    【解决方案1】:

    根据文档:

    如果现有单元格可用,此方法会将现有单元格出列,或根据您之前注册的类或 nib 文件创建新单元格。

    并继续:

    重要提示:在调用此方法之前,您必须使用 registerClass:forCellWithReuseIdentifier: 或 registerNib:forCellWithReuseIdentifier: 方法注册类或 nib 文件。

    您需要实现其中一种方法,因为dequeueReusableCellWithReuseIdentifier: forIndexPath: 可能需要创建一个对象并且需要上述方法来执行此操作。也许您已经创建了这些并且没有显示它们。如果你已经实现了其中之一,你能展示你的代码吗?

    更新:

    根据您的viewDidLoad 和您收到的错误消息,CellIdentifier 是NSString。当您调用[CellIdentifier class] 时,您正在将字符串注册为使用类,而不是单元格。你需要把你正在使用的类放在这里。根据您的实现,您似乎会使用[itemViewCell class]。

    请注意,您应该始终将类大写。因此,如果 itemViewCell 是一个实际的类,则应将其更改为 ItemViewCell,并且属性和变量名称应为小写(因此 cellIdentifer 与 CellIdentifier)。

    【讨论】:

    • 嗨,更新了问题,我确实在 viewdidload 中注册了该类
    猜你喜欢
    • 2019-08-28
    • 2012-07-24
    相关资源
    最近更新 更多