xcode – dequeueReusableCellWithReuseIdentifier crash’不能出现种类的视图“UICollectionElementKindCell”

前端之家收集整理的这篇文章主要介绍了xcode – dequeueReusableCellWithReuseIdentifier crash’不能出现种类的视图“UICollectionElementKindCell”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我遇到以下崩溃:

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

我的ViewDidLoad中有以下内容

[self.collectionView registerClass:[UICollectionViewCell class] 
        forCellWithReuseIdentifier:@"Cell"];

而崩溃的行在cellForItemAtIndexPath回调中:

UICollectionViewCell *cell = [collectionView 
    dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

我一直在搜索几个小时,但找不到任何解决方案.我试过子类化UICollectionViewCell但是得到相同的错误.

有断点我已经确定在执行dequeueReusableCellWithReuseIdentifier回调之前registerClass行被执行.

解决方法

我有这个问题,因为我正在调用registerClass之前,我正在实例化我的表视图对象.工作代码
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
[self.view addSubview:self.tableView];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView reloadData];
原文链接:https://www.f2er.com/iOS/335012.html

猜你在找的iOS相关文章