Joe
Joe

Reputation: 131

TableView with two custom cells causing cell re-use issue (Swift 4)

I'm displaying cells in a tableView between two custom cell types. Section-1 has several cells, Section-2 has one.

Section-1 cells have a label and a photo. The photo is downloaded with Alamofire.

Section-2 cells have a label and an empty photo with a colored background.

When the top cell in Section-1 scrolls off and is dequeued the Section-2 cell appears from the bottom and displays correctly.

But when I scroll the top cell back into view it appears to re-download its image. This ONLY happens if I have two custom cells. If I only have Section-1 cells and the first cell is scrolled off the image is already there and does not re-download when it comes into view again.

Any ideas why this is happening? The two cell types are different from each other yet it still seems cell 're-use' happening.

func numberOfSections(in tableView: UITableView) -> Int {

    sections = 0
    if condition1 == true {
        section1 = sections
    }
    if condition2 == true {
        sections += 1
        section2 = sections
    }
    return sections + 1

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if section1 == section {
        return array.count
    }
    if section2 == section {
        return 1
    }
    return 1

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cellSection1 = tableView.dequeueReusableCell(withIdentifier: "cellSection1", for: indexPath) as! Section1TableViewCell
    let cellSection2 = tableView.dequeueReusableCell(withIdentifier: "cellSection2", for: indexPath) as! Section2TableViewCell

    func loadImage(url:URL) {
        cellSection1.photo.af_setImage(withURL: url, placeholderImage: UIImage(), completion: { (response) -> Void in
            if response.result.error != nil && response.result.value == nil {
                cellSection1.officialPhoto.image = UIImage()
                cellSection2.photo.backgroundColor = .gray
            }
        })
    }
    if section1 == indexPath.section {

        let photoUrl = Foundation.URL(string: array[indexPath.row].photoUrl)
        cellSection1.name.text = array[indexPath.row].name
        if photoUrl != nil {
            loadImage(url:photoUrl)
        }
        return cellSection1

    }
    if section2 == indexPath.section {

        cellSection2.name.text = array[indexPath.row].name
        cellSection2.photo.image = UIImage()
        cellSection2.photo.backgroundColor = .blue
        return cellSection2

    }
    return cellSection1

}

Upvotes: 2

Views: 194

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100549

You can update to this with 2 sections assumption

func numberOfSections(in tableView: UITableView) -> Int {

    return 2

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if section == 0 {
        return array.count
    }

    return 1

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if(indexPath.section == 0){

         let cellSection1 = tableView.dequeueReusableCell(withIdentifier: "cellSection1", for: indexPath) as! Section1TableViewCell
        // configure cell

       return cellSection1 
    }
    else
    { 
         let cellSection2 = tableView.dequeueReusableCell(withIdentifier: "cellSection2", for: indexPath) as! Section2TableViewCell
         // configure cell


       return cellSection2 

    }

Upvotes: 1

Related Questions