首先使用集合视图,我遇到了以下错误:

由于未捕获的异常"NSInternalInconsistencyException"而终止应用程序,原因是:"无法将种类为:UICollectionElementKindCell的视图与标识符单元格一起出列-必须为标识符注册nib或类,或连接情节提要中的原型单元格"

代码非常简单,如下所示.我一辈子都搞不清楚我错过了什么.

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

集合视图控制器是使用nib和委托&数据源都设置为文件的所有者.

视图控制器的头文件也是非常基本的.

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

推荐答案

对于出列方法,从UICollectionView documentation开始:

Important:在调用此方法之前,必须使用registerClass:forCellWithReuseIdentifier:或RegisterInB:forCellWithReuseIdentifier:方法注册类或nib文件.

Objective-c相关问答推荐

Clang:将 id 之类的类型转换警告提升为 class

如何在没有 Interface Builder 的情况下创建 Cocoa 接口?

NSInternalInconsistencyException', reason: '试图将第 0 行插入第 0 节,但更新后第 0 节只有 0 行'

在哪里以及如何 __bridge

'if not' 的 Objective-C 预处理器指令

使用 NSArray 初始化对象数组

NSView 的边界与框架

如何在 iOS SDK 中使用语音识别?

UITableView - Select 了哪一行?

lldb 无法打印变量值并显示错误:对 'id' 的引用不明确

UILabel 的角半径属性在 iOS 7.1 中不起作用

为什么是 [object doSomething] 而不是 [*object doSomething]?

将 NSData 字节转换为 NSString?

MKAnnotation 图像偏移与自定义引脚图像

获取临时目录中文件的文件路径和 URL

以编程方式设置 UIView 的自动调整大小掩码?

从带有参数的方法名称创建 Select 器

UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效?

如何判断 WkWebView 是否在 Objective-C 中完成加载?

iOS 上的电话号码格式