GCD 在 UITableView 中返回不正确的结果
GCD Returning Incorrect Results in UITableView
我有一个加载到 UITableView 中的大约 8,500 个项目的列表,应该只有大约 200 个项目其中 product.isnewitem
为真。对于每个 'new' 项目,应该加载一个图像 (newicon.png) 以表明它是一个新项目;然而,当我开始在 table 视图上向下滚动时,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
}
希望对你有帮助
我有一个加载到 UITableView 中的大约 8,500 个项目的列表,应该只有大约 200 个项目其中 product.isnewitem
为真。对于每个 'new' 项目,应该加载一个图像 (newicon.png) 以表明它是一个新项目;然而,当我开始在 table 视图上向下滚动时,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
}
希望对你有帮助