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

时间:2017-03-31 23:42:29

标签: ios swift uitableview realm grand-central-dispatch

我有一个大约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;
}

1 个答案:

答案 0 :(得分:1)

我无法理解您使用此代码的原因

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

cellForRowAtIndexPath和队列中有两次,我认为您必须将此代码移至viewController viewDidLoadviewWillAppear,然后使用在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
}

我希望这有助于你