-1

I am having trouble presenting my tableview cells containing a Collectionview correctly. I want to display a different animal in each section based on the firebase data. My problem is about every 4 cells the first 4 cells repeat(See images below). I have separate models for each animal and I am configuring each cell with each animals information. Everything is loading fine except in the information being reused. For example:

Row 1 : chicken

**Displays chickens

Row 2: Cows

**Displays Cows

Row 3: Bees

**Displays Bees

Row 4: Goats

**Displays Goats

Row 5: Ducks

**Displays Chickens again, Then ducks

Row 6: Rabbits

**Displays Cows again, Then rabbits

This happens mostly when I scroll down so im guessing it has something to do with the dequeing of the cells. I put the indexpath.section to print and on section 4 I get 0? Im so confused! Any help would be greatly appreciated.

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = table.dequeueReusableCell(withIdentifier: MyCollectionTableViewCell.identifier, for: indexPath) as! MyCollectionTableViewCell
    switch indexPath.section {
        
    case 0:
        cell.chickenConfigure(with: chickenModels)
            return cell
    case 1:
        cell.cowConfigure(with: cowModels)
        return cell

    case 2:
        cell.beeConfigure(with: beeModels)
        return cell

    case 3:
        cell.donkeyConfigure(with: donkeyModels)
        return cell

    case 4:
        cell.duckConfigure(with: duckModels)
        return cell

    case 5:
        cell.goatConfigure(with: goatModels)
        return cell

    case 6:
        cell.horseConfigure(with: horseModels)
        return cell

    case 7:
        cell.pigConfigure(with: pigModels)
        return cell

    case 8:
        cell.rabbitConfigure(with: rabbitModels)
        return cell

        
    case 9:
        cell.sheepConfigure(with: sheepModels)
        
        
        return cell
        
    default:
        let cell = table.dequeueReusableCell(withIdentifier: MyCollectionTableViewCell.identifier, for: indexPath) as! MyCollectionTableViewCell
        cell.imageView?.image = UIImage(named: "Nil")
        return cell
    }
    
}

And in my Collectionview it looks like this:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    if indexPath.section == 0{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyCollectionViewCell.identifier, for: indexPath) as! MyCollectionViewCell
        if !chickenModels.isEmpty{
        cell.chickenConfigure(with: chickenModels[indexPath.row])
        return cell
        }
        else{
            cell.myImageView.image = nil
            return cell
        }
    }
    if indexPath.section == 1 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyCollectionViewCell.identifier, for: indexPath) as! MyCollectionViewCell
        if !cowModels.isEmpty {
        cell.cowConfigure(with: cowModels[indexPath.row])
        
        return cell
        }
        else{
            cell.isHidden = true
            return cell
        }
    }
    if indexPath.section == 2 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyCollectionViewCell.identifier, for: indexPath) as! MyCollectionViewCell
        if !beeModels.isEmpty {
        cell.beeConfigure(with: beeModels[indexPath.row])
        
        return cell
        }
        else{
            cell.isHidden = true
            return cell
        }
    }
    if indexPath.section == 3 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyCollectionViewCell.identifier, for: indexPath) as! MyCollectionViewCell
        if !donkeyModels.isEmpty {
        cell.donkeyConfigure(with: donkeyModels[indexPath.row])
        
        return cell
        }
        else{
            cell.isHidden = true
            return cell
        }
    }
    if indexPath.section == 4 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyCollectionViewCell.identifier, for: indexPath) as! MyCollectionViewCell
        if !duckModels.isEmpty {
        cell.duckConfigure(with: duckModels[indexPath.row])
        
        return cell
        }
        else{
            print("In the ELSE")
            cell.unusedConfigure(with: unusedModels[indexPath.row])
            return cell
        }
    }
    if indexPath.section == 5 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyCollectionViewCell.identifier, for: indexPath) as! MyCollectionViewCell
        if !goatModels.isEmpty {
        cell.goatConfigure(with: goatModels[indexPath.row])
        
        return cell
        }
        else{
            cell.isHidden = true
            return cell
        }
    }
    if indexPath.section == 6 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyCollectionViewCell.identifier, for: indexPath) as! MyCollectionViewCell
        if !horseModels.isEmpty {
        cell.horseConfigure(with: horseModels[indexPath.row])
        
        return cell
        }
        else{
            cell.isHidden = true
            return cell
        }
    }
    if indexPath.section == 7 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyCollectionViewCell.identifier, for: indexPath) as! MyCollectionViewCell
        if !pigModels.isEmpty {
        cell.pigConfigure(with: pigModels[indexPath.row])
        
        return cell
        }
        else{
            cell.isHidden = true
            return cell
        }
    }
    if indexPath.section == 8 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyCollectionViewCell.identifier, for: indexPath) as! MyCollectionViewCell
        if !rabbitModels.isEmpty {
        cell.rabbitConfigure(with: rabbitModels[indexPath.row])
        
        return cell
        }
        else{
            cell.isHidden = true
            return cell
        }
    }
    if indexPath.section == 9 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyCollectionViewCell.identifier, for: indexPath) as! MyCollectionViewCell
        if !sheepModels.isEmpty {
        cell.sheepConfigure(with: sheepModels[indexPath.row])
        
        return cell
        }
        else{
            cell.isHidden = true
            return cell
        }
    }
    else {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyCollectionViewCell.identifier, for: indexPath) as! MyCollectionViewCell
        
        cell.duckConfigure(with: duckModels[indexPath.row])
        return cell
    }
}
0

I got my answer! After reading through similar questions I came to a great solution that entailed clearing the model array that the cell was using before it was reused by using prepareForReuse(). Answer is in this thread: https://stackoverflow.com/questions/45247616/not-using-reusable-cell-in-uitableview-with-collectionview-in-each-cell

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.