如何阻止UITableView重用图像单元格数据?

lea*_*122 0 uitableview ios swift

我的Tableview重用以前的单元格图像数据并显示以前单元格中的图像.我做了以下以防止它重复使用.我错过了什么?

@IBAction func cityBtn(_ sender: UIButton) {
    for i in stride(from: 0, to: assignCities.count, by: 1)
       { cityName.append(bigCities[i])
         cityImages.append(bigCityImages[i])
        }}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let myCell= tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CityCell
    myCell.myLabel.text = cityName[indexPath.row]

   let cityImg = cityImages[indexPath.row]
    myCell.myImage.image = nil

    if (cityImg != "") 
    {
        myCell.myImage.image = UIImage(named:cityImg)
    }
    else 
    {
        myCell.myImage.image = nil
    }
    return myCell
}


import UIKit
class CityCell: UITableViewCell {
    @IBOutlet weak var myImage: UIImageView!
    @IBOutlet weak var myLabel: UILabel!

override func prepareForReuse() {
    super.prepareForReuse()
    myImage.image = nil
}

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}
}
Run Code Online (Sandbox Code Playgroud)

我做了以上但仍然没有得到结果

Vis*_*kar 7

您可以在那里实现func prepareForReuse()方法UITableViewCell并重置您的单元格属性.

在从UITableView方法dequeueReusableCell(withIdentifier :)返回对象之前调用此方法

 func prepareForReuse() {
     super. prepareForReuse()
     myImage.image = nil
 }
Run Code Online (Sandbox Code Playgroud)

这必须在自定义表视图单元类中实现.