gpt4 book ai didi

ios - UITableView (Swift 4) 中奇怪的缓存问题

转载 作者:行者123 更新时间:2023-11-28 09:29:00 24 4
gpt4 key购买 nike

我的应用程序遇到了一个非常奇怪的问题,我能想到的是它是某种缓存问题。基本上,当我滚动 UITableView 时,显示的缩略图会在同一行中多次重新加载,通常每次都使用不同的图像,然后最终显示在正确的图像上。

这里是一个简短的(20 秒)屏幕截图:https://youtu.be/oa04mlOgMeQ

应用程序应该在加载后缓存这些,并且图像被命名以匹配奖金代码(并且名称在 JSON 文件中)。我不明白为什么要这样做。

编辑:这是我的 cellForRowAt 代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "BonusListViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? BonusListViewCell else {
fatalError("The dequeued cell is not an instance of BonusListViewCell.")
}
// let bonus = bonuses[indexPath.row]
let bonus: JsonFile.JsonBonuses
if isFiltering() {
bonus = filteredBonuses[indexPath.row]
} else {
bonus = bonuses[indexPath.row]
}

let urlString = "http://tourofhonor.com/appimages/"+(bonus.imageName)
let url = URL(string: urlString)
cell.primaryImage.downloadedFrom(url: url!)
cell.nameLabel.text = bonus.name.capitalized
cell.bonusCodeLabel.text = bonus.bonusCode.localizedUppercase
cell.categoryLabel.text = bonus.category
cell.valueLabel.text = "\(bonus.value)"
cell.cityLabel.text = "\(bonus.city.capitalized),"
cell.stateLabel.text = bonus.state.localizedUppercase

return cell
}

这是我的 downloadedFrom 函数:

extension UIImageView {
func downloadedFrom(url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) {
contentMode = mode
URLSession.shared.dataTask(with: url) { data, response, error in
guard
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
let data = data, error == nil,
let image = UIImage(data: data)
else { return }
DispatchQueue.main.async() {
self.image = image
}
}.resume()
}
func downloadedFrom(link: String, contentMode mode: UIViewContentMode = .scaleAspectFit) {
guard let url = URL(string: link) else { return }
downloadedFrom(url: url, contentMode: mode)
}
}

我的 UITableViewCell 代码是:

import UIKit

class BonusListViewCell: UITableViewCell {

//MARK: Properties
@IBOutlet weak var bonusCodeLabel: UILabel!
@IBOutlet weak var categoryLabel: UILabel!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var valueLabel: UILabel!
@IBOutlet weak var cityLabel: UILabel!
@IBOutlet weak var stateLabel: UILabel!
@IBOutlet weak var flavorText: UITextView!
@IBOutlet weak var primaryImage: UIImageView!

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
}

}

最佳答案

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "BonusListViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? BonusListViewCell else {
fatalError("The dequeued cell is not an instance of BonusListViewCell.")
}
// let bonus = bonuses[indexPath.row]
let bonus: JsonFile.JsonBonuses
if isFiltering() {
bonus = filteredBonuses[indexPath.row]
} else {
bonus = bonuses[indexPath.row]
}

let urlString = "http://tourofhonor.com/appimages/"+(bonus.imageName)
let url = URL(string: urlString)

//set image url
cell.imageUrl = URL(string: images[indexPath.row])

cell.nameLabel.text = bonus.name.capitalized
cell.bonusCodeLabel.text = bonus.bonusCode.localizedUppercase
cell.categoryLabel.text = bonus.category
cell.valueLabel.text = "\(bonus.value)"
cell.cityLabel.text = "\(bonus.city.capitalized),"
cell.stateLabel.text = bonus.state.localizedUppercase

return cell
}

In your cell

import UIKit

class BonusListViewCell: UITableViewCell {

//MARK: Properties
@IBOutlet weak var bonusCodeLabel: UILabel!
@IBOutlet weak var categoryLabel: UILabel!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var valueLabel: UILabel!
@IBOutlet weak var cityLabel: UILabel!
@IBOutlet weak var stateLabel: UILabel!
@IBOutlet weak var flavorText: UITextView!
@IBOutlet weak var primaryImage: UIImageView!
@IBOutlet weak var activity: UIActivityIndicatorView!

var imageUrl: URL? {
didSet {
loadImage()
}
}
var task: URLSessionDataTask?

override func awakeFromNib() {
super.awakeFromNib()
clean()
}

func loadImage() {

//you need cancel previous task
task?.cancel()

clean()
guard let imageUrl = imageUrl else {
return
}
activity.startAnimating()
task = URLSession.shared.dataTask(with: imageUrl) { [weak self] data, response, error in
DispatchQueue.main.async {
self?.activity.stopAnimating()
if error == nil, let data = data {
self?.primaryImage.image = UIImage(data: data)
}
}
}
task?.resume()
}

func clean() {
primaryImage.image = nil
}
}

但最好使用像 AlamofireSDWebImage 这样的库

关于ios - UITableView (Swift 4) 中奇怪的缓存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51257406/

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