gpt4 book ai didi

ios - 如何阻止 UITableView 重复使用图像单元格数据?

转载 作者:行者123 更新时间:2023-11-28 09:36:19 25 4
gpt4 key购买 nike

我的 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
}
}

我做了上面的但是还是没有得到结果

最佳答案

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

This method is invoked just before the object is returned from the UITableView method dequeueReusableCell(withIdentifier:)

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

这必须在您的自定义表格 View 单元格类中实现。

关于ios - 如何阻止 UITableView 重复使用图像单元格数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41395633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com