UICollectionView中的iOS断言失败

Fog*_*ter 28 iphone ios uicollectionview

我收到了错误......

*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-2372/UICollectionView.m:2249
Run Code Online (Sandbox Code Playgroud)

尝试显示UICollectionView时.

引起它的线是......

static NSString *CellIdentifier = @"Cell";

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)

出列错误发生.

没有其他错误,所以我很难知道从哪里开始.

任何人都可以阐明这一点吗?

Gau*_*rav 37

你需要注册如下:

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MY_CELL"];
Run Code Online (Sandbox Code Playgroud)


Fog*_*ter 29

正在阅读文档(应该可能已经完成了这个:))

无论如何,我使用的collectionView是在一个单独的xib文件(不是故事板)和文档...

Important: You must register a class or nib file using the
registerClass:forCellWithReuseIdentifier: or
registerNib:forCellWithReuseIdentifier: method before calling this method.
Run Code Online (Sandbox Code Playgroud)

谢谢

  • 我有同样的问题,但由于一些奇怪的原因,编译器不能识别`viewDidLoad`中的`registerClass`方法,所以我不得不将它移动到`cellForItemAtIndexPath`方法. (2认同)