UICollectionView didSelectAt c разными ячейками
Есть UICollectionView в котором я использую две разных UICollectionViewCell
В зависимости от условий заполняю одной или второй ячейкой.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if (indexPath.row == 3 || count == 1)
{
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NoPetsViewCell.key, for: indexPath) as? NoPetsViewCell else {
return UICollectionViewCell()
}
return cell
} else {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: RatingViewCell.Key, for: indexPath) as? RatingViewCell else {
return UICollectionViewCell()
}
return cell
}
return UICollectionViewCell()
}
И проблема начинается когда пытаешься выделить ячейку.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NoPetsViewCell.key, for: indexPath) as? NoPetsViewCell {
presenter.presentAddPet()
} else {
presenter.presentDetailView(for: indexPath.row)
}
}
И при выделении любого типа ячейки он все равно считает что ячейка принадлежит к NoPetsViewCell и выполняет presenter.presentAddPet().
при использовании is вместо as ситуация не меняется.
В чем может быть проблема?
Ответы (1 шт):
Автор решения: Oleg Soloviev
→ Ссылка
Нужно просто проверить ячейку на соответствие нужному типу. Попробуйте так:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView.cellForItem(at: indexPath) as? NoPetsViewCell != nil {
presenter.presentAddPet()
} else {
presenter.presentDetailView(for: indexPath.row)
}
}