2012-09-29 39 views
29

我想使用UICollectionViewCell,因为我想显示的只是一个图像。我可以在UICollectionViewCellcontentView属性上使用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。那是因为我想改变视图的背景颜色。我不确定问题是什么,但有人可以看看我的代码并帮助我吗?我一直试图弄清楚这一点,绝对没有运气。

回答

60

如果您只想显示图像,则不需要执行任何子类化操作,则可以使用colorWithPatternImage:设置单元的backgroundColor。注册类是这样的:

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"]; 

然后使用它像这样:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor colorWithPatternImage:[self.results objectAtIndex:indexPath.row]]; 
    return cell; 
} 

在这个例子中,结果是UIImagesarray

8

尝试在

[self.collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"MyCell"]; 

设置断点我猜你的loadView(你的意思是viewDidLoad中?)方法不会被调用,因此该类不会与注册的CollectionView。

6

,如果你的集合视图连接在故事板和委托和数据源设置那里,你提供的数据源和委托必要的方法,然后将寄存器呼叫使

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath 

回报UICollectionView而不是你自己的子类。所以,要么但不是两者。

+1

谢谢!驱使我疯了:) –

+0

如果你想使用Xib,你可以在这个文档中找到答案[UICollectionView添加UICollectionCell](http://stackoverflow.com/questions/15184968/uicollectionview-adding-uicollectioncell)。 – Autobots

4

设置你的小区标识名称作为代码

enter image description here

+0

是的,这与注册viewDidLoad中的集合视图做了诀窍。我确实设法在出场电话中引起了一些伤心。我有“细胞”,即与一个无关的空白,这当然不是“细胞”相同,所以要非常小心你的名字。 – TJA

12

如果您使用的是applivation XIB然后添加你的viewDidLoad方法如下

[self.myCollectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CellIdentifier"]; 

否则,如果你使用故事板添加以下

[self.myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CellIdentifier"]; 

最后一个dd这个(如果没有)

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier" forIndexPath:indexPath]; 
return cell; 
} 

以上希望会有所帮助。

+2

谢谢,提醒我注册“Nib”,祝你好运! – Resty

+0

非常感谢。你的第一个片段是我一直在寻找的:) – Mexx

+0

我正在使用故事板&mt自定义单元格没有出列任何想法? –

相关问题