ios - 错误无法使 UICollectionElementKindCell 类型的 View 出队

标签 ios objective-c uicollectionview

首先尝试使用 Collection View 并遇到此错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

代码很简单,如下图。我一辈子都弄不明白我错过了什么。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor]; 
return cell;
}

Collection View Controller 是使用 nib 创建的,委托(delegate)和数据源都设置为文件的所有者。

View Controller 的头文件也很基础。

@interface NewMobialViewController_3 : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@end

最佳答案

来自UICollectionView documentation对于出队方法:

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

关于ios - 错误无法使 UICollectionElementKindCell 类型的 View 出队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15058975/

相关文章:

ios - 如何根据Firestore数据在collectionView中显示行和节标题?

ios - 当我更改数据源中的项目数时,UIcollection View 崩溃

ios - 使用 NSOutputStream 发送字节数组

objective-c - 在没有特定 NSTouchBar 支持的情况下创建 NSTouchBar 后,我可以将 NSTouchBar 附加到 NSWindow 吗?

ios - 如何使用 Objective-C 在 ios 9 中调用带有条件的 json

ios - 如果在 mainQueue/mainThread 之外运行,NavigationController.pushViewController 会崩溃

ios - 当 Collection View 最初占据屏幕的下半部分时,如何将其拉到整个屏幕?

objective-c - UIPrintInteractionController - 限制打印份数/获取打印份数

ios - 单点触控 iOS 7.0.2 中的 UIFont

iphone - 如何从 View 返回一个或多个值?