UICollectionView cellForItemAtIndexPath未注册单元格

dar*_*sky 31 xcode objective-c ipad ios uicollectionview

我正在尝试使用UICollectionViewCell,因为我想要显示的只是一个图像.我可以将图像添加到使用细胞UIColor colorWithImage:UICollectionViewCellcontentView财产.

在我的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;
}
Run Code Online (Sandbox Code Playgroud)

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

*** 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'
Run Code Online (Sandbox Code Playgroud)

我累了设置自定义单元格,并将其用作类,我得到了同样的错误.我的自定义单元子类化了UICollectionViewCell并且没有实现,默认情况除外initWithFrame.那是因为我想改变视图的背景颜色.我不确定问题是什么,但有人可以看看我的代码并帮助我吗?我一直在努力解决这个问题很长一段时间,完全没有运气.

rde*_*mar 60

如果你只是想显示图像,你不需要做任何的子类,你可以设置单元格backgroundColorcolorWithPatternImage:.像这样注册课程:

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

在这个例子中,结果是一个arrayUIImages.


Gag*_*iOS 14

如果您在applivation中使用xib,请在viewdidLoad方法中添加以下内容

    [self.myCollectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CellIdentifier"];
Run Code Online (Sandbox Code Playgroud)

否则如果您使用storyboard添加以下内容

  [self.myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CellIdentifier"];
Run Code Online (Sandbox Code Playgroud)

最后加上这个(如果不是)

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

希望以上会有所帮助.

  • 谢谢,提醒我注册"笔尖",祝你好运! (2认同)

小智 8

尝试设置断点

[self.collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"MyCell"];
Run Code Online (Sandbox Code Playgroud)

我猜你的loadView(你的意思是viewDidLoad?)方法没有被调用,因此该类从未在collectionView中注册.


Zso*_*olt 7

如果您的集合视图在storyboard上连接并且在那里设置了委托和数据源,并且您为数据源和委托提供了必要的方法,那么添加注册调用会使

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

返回一个UICollectionView而不是你自己的子类.所以要么做到,要么不是两者.