Hi all, I have this problem - Expected dequeued view to be returned to the collection view in preparation for display. When the collection view's data source is asked to provide a view for a given index path, ensure that a single view is dequeued and returned to the collection view. Avoid dequeuing views without a request from the collection view. For retrieving an existing view in the collection view, use -[UICollectionView cellForItemAtIndexPath:] or -[UICollectionView supplementaryViewForElementKind:atIndexPath:].
My code crashed just IOS18+
My code -
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SectionModsCell.cellIndetifire, for: indexPath) as? SectionModsCell else { return UICollectionViewCell() }
switch mainState {
case .main:
if ApphudManager.sharedInstance.isPremium {
let cellData = sectionModsData[indexPath.item]
return cell.setupCell(data: cellData)
} else {
let newIndex = getIndex(for: indexPath)
guard indexPath.item != adIndexPath || adIndexPath == 0 else {
if UIDevice.current.userInterfaceIdiom == .pad {
let bannerCell = collectionView.dequeueReusableCell(withReuseIdentifier: IpadNativeCell.cellIndetifire, for: indexPath) as! IpadNativeCell
bannerCell.setupCell(nativeAd: nativeAd)
return bannerCell
} else {
let bannerCell = collectionView.dequeueReusableCell(withReuseIdentifier: SmallNativeCell.cellIndetifire, for: indexPath) as! SmallNativeCell
bannerCell.setupCell(nativeAd: nativeAd)
return bannerCell
}
}
let cellData = sectionModsData[newIndex]
return cell.setupCell(data: cellData)
}
case .search:
let cellData = filteredModsData[indexPath.item]
return cell.setupCell(data: cellData)
}
}
And this code not give me error, my mind is **** (
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch mainState {
case .main:
if ApphudManager.sharedInstance.isPremium {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SectionModsCell.cellIndetifire, for: indexPath) as? SectionModsCell else { return UICollectionViewCell() }
let cellData = sectionModsData[indexPath.item]
return cell.setupCell(data: cellData)
} else {
let newIndex = getIndex(for: indexPath)
guard indexPath.item != adIndexPath || adIndexPath == 0 else {
if UIDevice.current.userInterfaceIdiom == .pad {
let bannerCell = collectionView.dequeueReusableCell(withReuseIdentifier: IpadNativeCell.cellIndetifire, for: indexPath) as! IpadNativeCell
bannerCell.setupCell(nativeAd: nativeAd)
return bannerCell
} else {
let bannerCell = collectionView.dequeueReusableCell(withReuseIdentifier: SmallNativeCell.cellIndetifire, for: indexPath) as! SmallNativeCell
bannerCell.setupCell(nativeAd: nativeAd)
return bannerCell
}
}
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SectionModsCell.cellIndetifire, for: indexPath) as? SectionModsCell else { return UICollectionViewCell() }
let cellData = sectionModsData[newIndex]
return cell.setupCell(data: cellData)
}
case .search:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SectionModsCell.cellIndetifire, for: indexPath) as? SectionModsCell else { return UICollectionViewCell() }
let cellData = filteredModsData[indexPath.item]
return cell.setupCell(data: cellData)
}
}