嗨,我有两个不同的单元格(将来可能是6-7 ),我在收集视图的cellForItem方法中返回它们,但是我得到了这个错误(从-collectionView:cellForItemAtIndexPath返回的单元格:不具有reuseIdentifier -单元格必须通过调用来检索)
这是我的密码。
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
}发布于 2022-01-11 10:29:16
说这话是合法的
return UICollectionViewCell()以便使编译器安静下来,但是执行它是非法的。您正在执行它,因为您的条件中的两个分支都没有返回单元格。
你说的是双翼的let cell =,然后你就把cell扔掉了!你从来没说过return cell。您必须将一个单元格排出队列,配置它,并返回它。不要返回其他单元;返回您已退出队列和配置的单元,即cell。
https://stackoverflow.com/questions/70664936
复制相似问题