【问题标题】:Error with custom UICollectionViewCell自定义 UICollectionViewCell 出错
【发布时间】:2012-12-24 10:41:20
【问题描述】:

我正在尝试设置UICollectionView。起初我使用的是基本的UICollectionViewCell,它运行良好,正在显示单元格。

然后我创建了自己的 collectionViewCell(子类化UICollectionViewCell),以便能够在单元格中显示图像。 我将 Storyboard 中的自定义单元格与一个标识符相关联,并且我还为它指定了正确的类。

但是,我一直有这个错误,我不明白为什么:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier MediaCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

我设法通过在viewDidLoad 中添加这一行来解决这个错误,但在我关注的教程中,这些人没有使用它:

[self.collectionView registerClass:[MediaCollectionViewCell class] forCellWithReuseIdentifier:@"MediaCell"];

问题是,如果我添加它,collectionView仍然是黑色的,没有数据显示。

【问题讨论】:

    标签: iphone ios cocoa-touch uicollectionview uicollectionviewcell


    【解决方案1】:

    我昨天遇到了这个确切的问题(因为文档说您必须注册该类),但是文档不准确,因为如果您的情节提要中存在原型单元并且您给它一个标识符,则您不应该注册。

    如果您在情节提要中创建UICollectionViewCell 的原型并分配您的自定义单元格类,则没有必要(如果这样做是有害的)注册自定义UICollectionViewCell。这就是生成的UICollectionView 是黑色的原因。

    根据错误消息,情节提要中的原型单元格的标识符设置不正确(根本没有设置或不是MediaCell

    【讨论】:

    • 我在情节提要中分配了自定义单元格类,并将其命名为 MediaCell。奇怪的是情节提要中的原型单元确实设置了标识符,这就是我不理解错误的原因。谢谢!
    • 非常感谢你...花了 2 小时试图弄清楚为什么它没有拿起我的原型。
    【解决方案2】:

    你是对的使用这条线

    [self.collectionView registerClass:[MediaCollectionViewCell class] forCellWithReuseIdentifier:@"MediaCell"]; 
    

    因为这有助于细胞重复使用。

    问题可能出在您的cellForRowAtIndexPath 方法、您的子类或您设置委托的方式上。可以发一下代码吗?

    您的委托方法应如下所示:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        MediaCollectionViewCell* cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"MediaCell" forIndexPath:indexPath];
        // set cell.image
        return cell;
    }
    

    你应该在你的 viewDidLoad 方法中设置你的委托...

    [self.collectionView setDataSource:self];
    [self.collectionView setDelegate:self];
    

    编辑: 您可以发布 MediaCollectionViewCell 实现的代码吗?我做了类似的事情,我的看起来像这样。

    - (id)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self.contentView addSubview:self.imageView];
    }
    return self;
    

    }

    【讨论】:

    • 我在情节提要中设置了委托和数据源。这是cellForItemAtIndexPath- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { MediaCollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MediaCell" forIndexPath:indexPath]; NSURL *imageURL = [NSURL URLWithString:[[[self.mediaModel.response objectForKey:@"list"]objectAtIndex:indexPath.row]objectForKey:@"smallImgUrl"]]; [[cell imageView]setImage:[UIImage imageNamed:@"Default.png"]]; return cell; }的代码
    • 自定义单元格的代码:#import <UIKit/UIKit.h> @interface MediaCollectionViewCell : UICollectionViewCell @property (nonatomic, strong) IBOutlet UIImageView *imageView; @end 谢谢!
    • 得到NSURL *imageURL后,你在哪里使用呢?似乎您只是将单元格设置为 Default.png,然后返回该单元格。您是否以任何方式使用 imageURL?您需要从 imageURL 下载 UIImage,然后将其设置为单元格的 imageView 的图像。
    • 我实际上使用 Default.png 只是为了检查问题与图像下载无关。我认为我面临的问题是因为我使用的 TabBarController 设置不正确。
    • 查看我编辑过的关于 MediaCollectionViewCell.m 中的 initWithFrame 方法的帖子
    【解决方案3】:

    这实际上是与 TabBarController 相关的问题。我没有在其中显示正确的类,这就是 collectionView 总是为空的原因。 我在这里发布的代码正在运行。感谢大家的帮助。

    【讨论】:

      猜你喜欢
      • 2018-06-03
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多