我有一个在UITableView中加载的大约8500个项目的列表,其中只有大约200个项目product.isnewitem是真的。对于每个“new”项,都应该加载一个图像(newicon.png),指示它是一个新项;但是,当我开始在表视图上向下滚动时,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中两次,在队列中一次,我认为您必须将此代码移动到您的viewControllerviewDidLoadviewWillAppear中,然后使用在您的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")
}

在你的牢房里你应该有这样的东西
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
}

我希望这对你有帮助

07-26 01:03