我正在尝试使用UICollectionViewCell
,因为我想要显示的只是一张图片。我可以使用'属性将图像添加到单元格中UIColor colorWithImage:
。UICollectionViewCell
contentView
在我的loadView
方法中,我正在注册单元格,如下所示:
[self.collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"MyCell"];
下面是我的cellForItemAtIndexPath
方法:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
// cell customization
return cell;
}
当我运行它时,一旦它到达出队行,它就会崩溃并出现以下错误:
*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:]
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier MyCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
我厌倦了设置自定义单元格,并将其用作类,我得到了同样的错误。我的自定义单元格是 UICollectionViewCell 的子类,除了默认的initWithFrame
. 那是因为我只想更改视图的背景颜色。我不确定问题是什么,但有人可以看看我的代码并帮助我吗?我一直试图弄清楚这一点,但完全没有运气。