UICollectionView cellForItemAtIndexPath未注册单元格 [英] UICollectionView cellForItemAtIndexPath not registering cell

查看:210
本文介绍了UICollectionView cellForItemAtIndexPath未注册单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 UICollectionViewCell ,因为我想要显示的只是一张图片。我可以在 UICollectionViewCell contentView上使用 UIColor colorWithImage:将图像添加到单元格 property。

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.

在我的 loadView 方法中,我按如下方式注册单元格:
[self.collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@MyCell];

In my loadView method, I am registering the cell as follows: [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;
}

当我运行它时,一旦它到达出列线,它就会崩溃出现以下错误:

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'

我厌倦了设置一个自定义单元格,并将其用作类,我得到了同样的错误。我的自定义单元子类化了UICollectionViewCell并且没有实现,除了默认的 initWithFrame 。那是因为我想改变视图的背景颜色。我不确定问题是什么,但有人可以看看我的代码并帮助我吗?我一直试图弄清楚这一点,绝对没有运气。

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.

推荐答案

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

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:

[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;
}

在这个例子中,结果是数组 UIImages

In this example, results is an array of UIImages.

这篇关于UICollectionView cellForItemAtIndexPath未注册单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆