首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSInternalInconsistencyException中来自collectionView的错误

NSInternalInconsistencyException中来自collectionView的错误
EN

Stack Overflow用户
提问于 2022-01-11 10:09:55
回答 1查看 165关注 0票数 -1

嗨,我有两个不同的单元格(将来可能是6-7 ),我在收集视图的cellForItem方法中返回它们,但是我得到了这个错误(从-collectionView:cellForItemAtIndexPath返回的单元格:不具有reuseIdentifier -单元格必须通过调用来检索)

这是我的密码。

代码语言:javascript
复制
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if let idx = viewModel?.component?.firstIndex(where: { $0.type == .summary }) {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SummaryCollectionViewCell.identifier, for: indexPath) as? SummaryCollectionViewCell else { return UICollectionViewCell() }
        if let summaryData = viewModel?.component?[idx].dataModel as? SummaryData {
            let summaryViewModel = SummaryCollectionViewCellViewModel(summaryData: summaryData)
            cell.configure(with: summaryViewModel)
        }
    } else if let idx = viewModel?.component?.firstIndex(where: { $0.type == .merchant }) {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MerchantCollectionViewCell.identifier, for: indexPath) as? MerchantCollectionViewCell else { return UICollectionViewCell() }
        if let merchantData = viewModel?.component?[idx].dataModel as? MerchantData {
            let merchantViewModel = MerchantCollectionViewCellViewModel(merchantData: merchantData)
            cell.configure(with: merchantViewModel)
        }
    }
    
    return UICollectionViewCell()
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 2
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-11 10:29:16

说这话是合法的

代码语言:javascript
复制
return UICollectionViewCell()

以便使编译器安静下来,但是执行它是非法的。您正在执行它,因为您的条件中的两个分支都没有返回单元格。

你说的是双翼的let cell =,然后你就把cell扔掉了!你从来没说过return cell。您必须将一个单元格排出队列,配置它,并返回它。不要返回其他单元;返回您已退出队列和配置的单元,即cell

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70664936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档