gpt4 book ai didi

ios - GCD 在 UITableView 中返回不正确的结果

转载 作者:行者123 更新时间:2023-11-28 15:45:27 24 4
gpt4 key购买 nike

我有一个加载到 UITableView 中的大约 8,500 个项目的列表,应该只有大约 200 个项目,其中 product.isnewitem 为 true。对于每个"new"项目,应该加载一个图像 (newicon.png) 以表明它是一个新项目;然而,当我开始在表格 View 上向下滚动时,newicon 显示在超过 50% 的项目上。所有项目都通过 Realm 加载。

新项目的检查在以下时间完成:

if product.isnewitem {
cell.newIconImageView.image = #imageLiteral(resourceName: "newicon.png")
}

这是整个 cellForRowAtIndexPath 方法:

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

let paths = NSSearchPathForDirectoriesInDomains(documentsDirectory, userDomainMask, true)

let cell = tableView.dequeueReusableCell(withIdentifier: "ProductCell") as? OrderFormViewCell
?? UITableViewCell(style: .subtitle, reuseIdentifier: "ProductCell") as! OrderFormViewCell

let realm = try! Realm()
let allProducts = realm.objects(Product.self).sorted(byKeyPath: "basedescription")
let product = allProducts[indexPath.row]

cell.productDescriptionLabel.text = product.basedescription

queue.async {
let realm = try! Realm()

let allProducts = realm.objects(Product.self).sorted(byKeyPath: "basedescription")
let product = allProducts[indexPath.row]

if product.isnewitem {
cell.newIconImageView.image = #imageLiteral(resourceName: "newicon.png")
}

if let dirPath = paths.first {
let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent("T\(product.itemno.replacingOccurrences(of: "-", with: "")).png")

if let image = UIImage(contentsOfFile: imageURL.path) {
cell.productImageView.image = image
}
else {
cell.productImageView.image = #imageLiteral(resourceName: "image-coming-soon.png")
}
}


}



return cell
}

最佳答案

我不明白你为什么要用这个代码

let realm = try! Realm()
let allProducts = realm.objects(Product.self).sorted(byKeyPath: "basedescription")
let product = allProducts[indexPath.row]

在你的 cellForRowAtIndexPath 中有两次,在队列中有一次,我认为你必须将这段代码移动到你的 viewController viewDidLoad 中,或者 viewWillAppear,然后使用在您的 viewController

上声明的本地数组中的产品
var allProducts : Results<Product>?

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let realm = try! Realm()
self.allProducts = realm.objects(Product.self).sorted(byKeyPath: "basedescription")
}

在你的 cellForRowAtIndexPath 中你应该有这样的东西

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

let paths = NSSearchPathForDirectoriesInDomains(documentsDirectory, userDomainMask, true)

let cell = tableView.dequeueReusableCell(withIdentifier: "ProductCell") as? OrderFormViewCell
?? UITableViewCell(style: .subtitle, reuseIdentifier: "ProductCell") as! OrderFormViewCell

let product = self.allProducts[indexPath.row]
cell.productDescriptionLabel.text = product.basedescription
if product.isnewitem {
cell.newIconImageView.image = #imageLiteral(resourceName: "newicon.png")
}
else {
cell.newIconImageView.image = nil
}

if let dirPath = paths.first {
let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent("T\(product.itemno.replacingOccurrences(of: "-", with: "")).png")

if let image = UIImage(contentsOfFile: imageURL.path) {
cell.productImageView.image = image
}
else {
cell.productImageView.image = #imageLiteral(resourceName: "image-coming-soon.png")
}
}

return cell
}

希望对你有帮助

关于ios - GCD 在 UITableView 中返回不正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43151124/

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