ios UICollectionView cellForItemAtIndexPath 未注册单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12657421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UICollectionView cellForItemAtIndexPath not registering cell
提问by darksky
I am trying to use UICollectionViewCell
, since all I want to display is an image. I can add the image to the cell using UIColor colorWithImage:
on the UICollectionViewCell
's contentView
property.
我正在尝试使用UICollectionViewCell
,因为我只想显示一个图像。我可以将图像添加到使用细胞UIColor colorWithImage:
上UICollectionViewCell
的contentView
财产。
In my loadView
method, I am registering the cell as follows:
[self.collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"MyCell"];
在我的loadView
方法中,我按如下方式注册单元格:
[self.collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"MyCell"];
Below is my cellForItemAtIndexPath
method:
下面是我的cellForItemAtIndexPath
方法:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
// cell customization
return cell;
}
When I run it, as soon as it hits the dequeue line, it crashes with the following error:
当我运行它时,只要它碰到出队线,它就会崩溃并显示以下错误:
*** 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'
I tired setting up a custom cell, and used it as the class and I got the same error. My custom cell subclassed UICollectionViewCell and had nothing implemented, except for the default initWithFrame
. That is because I wanted to just change the background colour of the view. I am not sure what the problem is but could someone please take a look at my code and help me? I've been trying to figure this out for quite a while with absolutely no luck at all.
我厌倦了设置自定义单元格,并将其用作类,但我遇到了同样的错误。我的自定义单元格继承了 UICollectionViewCell 并且除了默认的initWithFrame
. 那是因为我只想更改视图的背景颜色。我不确定问题是什么,但有人可以看看我的代码并帮助我吗?我一直在努力解决这个问题,但完全没有运气。
回答by rdelmar
If you just want to display an image, you don't need to do any subclassing, you can set the cell's backgroundColor
with colorWithPatternImage:
. Register the class like this:
如果你只是想显示图像,你不需要做任何的子类,你可以设置单元格backgroundColor
用colorWithPatternImage:
。像这样注册类:
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
Then use it like so:
然后像这样使用它:
- (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;
}
In this example, results is an array
of UIImages
.
在这个例子中,结果是一个array
的UIImages
。
回答by Gagan_iOS
If you are using xib in applivation then add following in your viewdidLoad method
如果您在 applivation 中使用 xib,则在您的 viewdidLoad 方法中添加以下内容
[self.myCollectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CellIdentifier"];
otherwise If you using storyboard add following
否则,如果您使用故事板添加以下内容
[self.myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CellIdentifier"];
Finally add this (If not)
最后添加这个(如果没有)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier" forIndexPath:indexPath];
return cell;
}
Hope above will help.
希望以上会有所帮助。
回答by Charlie Elliott
Try setting a breakpoint on
尝试设置断点
[self.collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"MyCell"];
I would guess your loadView (did you mean viewDidLoad?) method is not being called, so the class is never registered with the collectionView.
我猜你的loadView(你的意思是viewDidLoad?)方法没有被调用,所以这个类永远不会注册到collectionView。
回答by Zsolt
if your collection view is connected on storyboard and the delegate and datasource is set there, and you provide the necessary methods for the datasource and delegate, then adding the register call makes the
如果您的集合视图连接在情节提要上并且在那里设置了委托和数据源,并且您为数据源和委托提供了必要的方法,则添加注册调用会使
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
return a UICollectionView
instead of your own subclass of it. So do either but not both.
返回一个UICollectionView
而不是你自己的子类。所以要么做,但不要同时做。
回答by codercat
set your cell identifier name as in code
将您的单元标识符名称设置为代码