首先尝试使用 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/