Reusable cell old image showing

后端 未结 6 830
醉话见心
醉话见心 2021-01-12 07:30

I am facing a problem when scrolling from top to bottom of my tableview. The reusable cell shows old image until new image download is completed.

It sh

6条回答
  •  盖世英雄少女心
    2021-01-12 08:04

    just as i had the same problem; before reading this. i tried different ways such as the init and awakefromnib. the way to do it is just as soon as you receive the data before calling the func to update the whole UI.

    Also, note im using a var restData and the didSet (this why it updates as soon as receiving the data from the viewcontroller, im not calling the func. if it was a func to call from the viewcontroller you would just put the placeholder image initially at the beginning of the func before downloading the new image and call it from the viewcontroller such as cell.updateUI(data: data)

    Hope you guys find this useful. for me, im getting correct images without showing the preview image on the cell reuse :)

    // tableview controller

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    
        let cell : RestCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! RestCell
    
        //configure the cell to item
       cell.restData = items[indexPath.row]
    
        return cell
    }
    

    // UITableviewCell

     class RestCell: UITableViewCell {
    
        @IBOutlet var img : UIImageView!
        @IBOutlet var lbl : UILabel!
    
        // get data and update.
        var restData: RestsModel!  {
            didSet {
                // default img early placeholder set. Eliminates showing old pic in cell reuse
                self.img.image = UIImage(named: "60x60placeholder.png")
                // Update all values func
                updateUI()
            }
        }
    
        override func awakeFromNib() {
            super.awakeFromNib()
            // Default image placeholder
           //self.img.image = UIImage(named: "60x60placeholder.png")
    
        }
        // required init.
        required init(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)!
    
            // Color of selected.
            let myCustomSelectionColorView = UIView()
            myCustomSelectionColorView.backgroundColor = UIColor(red: 234.0/255.0, green: 46.0/255.0, blue: 73.0/255.0, alpha: 0.1) //UIColor(netHex:0xFFFF434)
            selectedBackgroundView = myCustomSelectionColorView
    
    
    
        }
    
    
        // updateUI func.
        fileprivate func updateUI() {
    
           // download the image
            if ( self.restData.image != nil  &&  self.restData.image != "") {
                ImageLoader.sharedLoader.imageForUrl(urlString:self.restData.image, completionHandler:{(image: UIImage?, url: String) in
                    self.img.contentMode = UIViewContentMode.scaleAspectFit
                    self.img.image = image
                })
            } else {
                self.img.image = UIImage(named: "60x60placeholder.png")
            }
    
    
            self.lbl.text = restData.name as String
    
    
    
        }
    
    }
    

提交回复
热议问题