gpt4 book ai didi

ios - Storyboard、UICollectionview、发送到实例的无法识别的选择器

转载 作者:行者123 更新时间:2023-11-28 20:17:38 26 4
gpt4 key购买 nike

我有一个托管 UICollectionview 的 Storyboard,在 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 方法上放置了一个断点并且它从不加热它。我做错了什么。

最佳答案

根据文档:

This method dequeues an existing cell if one is available or creates a new one based on the class or nib file you previously registered.

继续:

Important: You must register a class or nib file using the registerClass:forCellWithReuseIdentifier: or registerNib:forCellWithReuseIdentifier: method before calling this method.

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

更新:

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

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

关于ios - Storyboard、UICollectionview、发送到实例的无法识别的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17325457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com