我想使用UICollectionViewCell
,因为我想显示的只是一个图像。我可以在UICollectionViewCell
的contentView
属性上使用UIColor colorWithImage:
将图像添加到单元格。UICollectionView cellForItemAtIndexPath不注册单元格
在我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
。那是因为我想改变视图的背景颜色。我不确定问题是什么,但有人可以看看我的代码并帮助我吗?我一直试图弄清楚这一点,绝对没有运气。
谢谢!驱使我疯了:) –
如果你想使用Xib,你可以在这个文档中找到答案[UICollectionView添加UICollectionCell](http://stackoverflow.com/questions/15184968/uicollectionview-adding-uicollectioncell)。 – Autobots