嗨,我有兩個不同的單元格(將來可能是 6-7)我在某些情況下在集合視圖的 cellForItem 方法中回傳它們,但我收到了這個錯誤(從 -collectionView:cellForItemAtIndexPath 回傳的單元格:沒有重用識別符號 -必須通過呼叫來檢索單元格)
這是我的代碼。
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
}
uj5u.com熱心網友回復:
說是合法的
return UICollectionViewCell()
為了安靜的編譯器,但它是非法來執行它。您正在執行它,因為您的條件的任何一方都沒有回傳 cell。
你說let cell =的是兩翼,但你只是扔掉cell!你從來不說return cell。您必須使一個單元出隊、配置它并回傳它。不要回傳其他單元格;回傳您出列和配置的單元格,即cell.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/408269.html
標籤:
