我有一个大约8,500个项目的列表,这些项目在UITableView中加载,并且应该只有大约200个项目if product.isnewitem {
cell.newIconImageView.image = #imageLiteral(resourceName: "newicon.png")
}
为真。对于每个“新”项,应该加载一个图像(newicon.png),表明它是一个新项;但是,当我开始向下滚动表格视图时,newicon会显示超过50%的项目。所有项目都通过Realm加载。
检查新项目是在:
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
}
这是整个cellForRowAtIndexPath方法:
.shinysky-busy-indicator {
z-index: 1000;
}
答案 0 :(得分:1)
我无法理解您使用此代码的原因
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
}
我希望这有助于你