You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS中用Objective-C创建UICollectionView时遇单元格返回nil异常

问题:UICollectionView返回nil单元格导致崩溃

我一直在尝试在iOS中正确实现UICollectionView,参考了自定义UICollectionViewCell教程。我认为大部分代码都正确,完全使用了教程中的自定义单元格类,但进入包含该UICollectionView的视图控制器时持续报错。

以下是我的代码:

#import "BackPackController.h"
#import "UICollectionViewCell+CollectionViewCell.h"

@interface BackPackController () <UICollectionViewDelegate, UICollectionViewDataSource>

@end

@implementation BackPackController

NSArray *itemArray; //our item array

- (void)viewDidLoad {
    [super viewDidLoad];
    itemArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"PlayerItems"]; //get the players items from the user defaults and make an array of them
    _itemGrid.delegate = self;
    _itemGrid.dataSource = self;
    //[_itemGrid registerClass:CollectionViewCell.self forCellWithReuseIdentifier:@"cell1"];

}


- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    NSLog(@"Got this far");
    CollectionViewCell *cell1 = [_itemGrid dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
    [cell1.customLabel setText:itemArray[indexPath.row]];
    return cell1;
    
}



- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
    return itemArray.count;
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:   (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(100, 100);
}

我已正确设置控制器为代理和数据源,上述代码中的NSLog能打印信息,说明cellForItemAtIndexPath方法已被调用,但调用后立刻出现以下错误:

2023-11-16 08:36:19.163928+1300 Matoran Quest[7698:1793467] *** Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:], UICollectionView.m:3389
2023-11-16 08:36:19.164525+1300 Matoran Quest[7698:1793467] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the collection view's data source returned nil from -collectionView:cellForItemAtIndexPath: for index path <NSIndexPath: 0x8db3e297ef8cf73e> {length = 2, path = 0 - 0}'

被访问的列表是简单字符串数组,且在其他地方能正常访问,我尝试将单元格文本改为@"test"也无济于事。


解决方案

崩溃的核心原因是UICollectionView无法找到对应复用标识符的单元格模板,导致dequeueReusableCellWithReuseIdentifier:forIndexPath:返回nil,最终触发断言失败。

1. 修复单元格注册逻辑

你代码中注释掉了注册单元格的关键代码:

//[_itemGrid registerClass:CollectionViewCell.self forCellWithReuseIdentifier:@"cell1"];
  • 如果单元格是纯代码创建:取消注释这行代码,确保CollectionView能识别cell1对应的单元格类。
  • 如果单元格是Storyboard/XIB创建:不需要调用registerClass:,但必须在Storyboard中完成两个设置:
    • 选中UICollectionViewCell,在属性面板的Identifier字段填写cell1
    • 将单元格的Class设置为CollectionViewCell

2. 修正导入语句

你的导入语句存在格式问题:

#import "UICollectionViewCell+CollectionViewCell.h"

如果CollectionViewCell是独立子类(不是UICollectionViewCell的分类),正确的导入应该是:

#import "CollectionViewCell.h"

错误的导入会导致编译器无法识别CollectionViewCell类,进而注册失败。

3. 额外验证点

  • 确认itemArray不为空:在viewDidLoad中添加NSLog(@"itemArray count: %lu", (unsigned long)itemArray.count),避免数组为空引发后续问题(当前崩溃并非此原因,但可提前排查)
  • 检查_itemGrid的IBOutlet关联:确保它已正确绑定到Storyboard中的UICollectionView控件,没有为空的情况

按照上述步骤修复后,dequeueReusableCellWithReuseIdentifier:forIndexPath:就能正确返回单元格实例,崩溃问题即可解决。

内容的提问来源于stack exchange,提问作者Jobalisk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 23:17:38