【发布时间】:2013-02-25 01:55:12
【问题描述】:
首先使用集合视图并遇到此错误:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法将类型视图出列:带有标识符 Cell 的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类,或者在情节提要中连接一个原型单元格”
代码很简单,如下图。我一生都无法弄清楚我错过了什么。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
集合视图控制器是使用 nib 创建的,并且委托和数据源都设置为文件的所有者。
View Controller 的头文件也很基础。
@interface NewMobialViewController_3 : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@end
【问题讨论】:
-
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
-
不,如果使用 NIB,请使用
registerNib:forCellWithReuseIdentifier:。但是,如果在没有 NIB 或情节提要单元原型的情况下以编程方式构建单元,那么,是的,使用registerClass:forCellWithReuseIdentifier:。
标签: ios objective-c uicollectionview