ios - CollectionViewCell 中标签中的数据有时会在重新加载时刷新,有时则不会

标签 ios swift uicollectionview uilabel uicollectionviewcell

首先我要说的是,这似乎是 SO 上的一个常见问题,我已经阅读了从 Swift 到 Obj-C 能找到的每一篇文章。在过去 9 小时内我尝试了很多不同的方法,但我的问题仍然存在。

我有一个 vc (vc1),里面有一个 collectionView。在 collectionView 内部,我有一个自定义单元格,其中有一个标签和一个 imageView 。在 cellForItem 内部,我有一个属性,该属性也在自定义单元格内部,当从 datasource[indePath.item] 设置该属性时,单元格内部有一个属性观察器,用于设置标签和 imageView 的数据.

vc1 中有一个按钮可以按下 vc2,如果用户从 vc2 中选择某些内容,它就会通过委托(delegate)传递回 vc1。 vc2 被弹出。

正确的数据总是会被传回(我在调试器中检查了多次)。

问题是,如果 vc1 中有一个现有单元格,当新数据添加到数据源时,在我重新加载 collectionView 后,第一个单元格中的标签数据现在显示在新单元格和数据中的标签上新单元现在显示在旧单元的标签上。

我已经尝试了从prepareToReuse到删除标签的所有方法,但由于某种原因,只有单元格的标签数据变得困惑。奇怪的是有时标签会正确更新,有时则不会imageView 始终显示正确的图像,即使标签数据不正确,我也不会遇到任何问题。数据源内的 2 个模型对象始终位于正确的索引位置并具有正确的信息。

可能是什么问题?

vc1: UIViewController, CollectionViewDataSource & Delegate {

    var datasource = [MyModel]() // has 1 item in it from viewDidLoad

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: customCell, for: indexPath) as! CustomCell

        cell.priceLabel.text = ""
        cell.cleanUpElements()
        cell.myModel = dataSource[indexPath.item]

        return cell
    }

    // delegate method from vc2
    func appendNewDataFromVC2(myModel: MyModel) {

        // show spinner

        datasource.append(myModel) // now has 2 items in it

        // now that new data is added I have to make a dip to fb for some additional information
        firebaseRef.observeSingleEvent(of: .value, with: { (snapshot) in

            if let dict = snapshot.value as? [String: Any] else { }

            for myModel in self.datasource {
                myModel.someValue = dict["someValue"] as? String
            }

            // I added the gcd timer just to give the loop time to finish just to see if it made a difference
            DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {

               self.datasource.sort { return $0.postDate > $1.postDate } // Even though this sorts correctly I also tried commenting this out but no difference
               self.collectionView.reloadData()

               // I also tried to update the layout
               self.collectionView.layoutIfNeeded()

               // remove spinner
            }
        })
    }    
}

下面的自定义单元格。这是 myModel 属性观察器内部内容的简化版本。标签中显示的数据取决于其他数据,并且有一些条件可以确定它。在 cellForItem 内添加所有这些内容会创建一堆代码,这就是为什么我没有更新其中的数据(或在此处添加)并选择在单元格内执行此操作。但正如我之前所说,当我检查数据时,它总是 100% 正确的。属性观察器始终正常工作。

CustomCell: UICollectionViewCell {

    let imageView: UIImageView = {
        let iv = UIImageView()
        iv.translatesAutoresizingMaskIntoConstraints = false
        return iv
    }()

    let priceLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    var someBoolProperty = false

    var myModel: MyModel? {
        didSet {

           someBoolProperty = true

           // I read an answer that said try to update the label on the main thread but no difference. I tried with and without the DispatchQueue
           DispatchQueue.main.async { [weak self] in
                self?.priceLabel.text = myModel.price!
                self?.priceLabel.layoutIfNeeded()  // tried with and without this
           }

           let url = URL(string: myModel.urlStr!)
           imageView.sd_setImage(with: url!, placeholderImage: UIImage(named: "placeholder"))

           // set imageView and priceLabel anchors
           addSubview(imageView)
           addSubview(priceLabel)

           self.layoutIfNeeded() // tried with and without this
        }
    }

    override func prepareForReuse() {
        super.prepareForReuse()

        // even though Apple recommends not to clean up ui elements in here, I still tried it to no success
        priceLabel.text = ""
        priceLabel.layoutIfNeeded() // tried with and without this
        self.layoutIfNeeded() // tried with and without this

        // I also tried removing the label with and without the 3 lines above 
        for view in self.subviews {
            if view.isKind(of: UILabel.self) {
                view.removeFromSuperview()
            }
        }
    }

    func cleanUpElements() {
        priceLabel.text = ""
        imageView.image = nil
    }
}

我在添加 priceLabel.text = "" 的所有位置添加了 1 个断点(总共 3 个),一旦 collectionView 重新加载,断点总是会被击中 6 次(对于 2 个对象来说,击中 3 次)第一次在 prepareForReuse 中,第二次在 cellForItem 中,第三次在 cleanUpElements()

最佳答案

结果我必须重置单元格内的属性。尽管单元格被重用并且priceLabel.text被清除,该属性仍然保持其旧的bool值。一旦我通过 cellForItem 重置它,问题就消失了。

10 小时,smh

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: customCell, for: indexPath) as! CustomCell

    cell.someBoolProperty = false
    cell.priceLabel.text = ""
    cell.cleanUpElements()
    cell.myModel = dataSource[indexPath.item]

    return cell
}

关于ios - CollectionViewCell 中标签中的数据有时会在重新加载时刷新,有时则不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53982323/

相关文章:

ios - 在 UIAlertController Action 中关闭 UIViewController 会导致崩溃

swift - 如何使用 Firebase 更新 CollectionView 模型?

ios - Swift 4 UIWebView 视频全屏约束错误

javascript - 在 NodeJS/JavaScript 中使用 iOS Swift Date()

swift - 在单个 If 语句中检查 2 个 NSTextFields 中的值是否为正整数

ios - uicollectionview 继续详细查看 viewcontroller swift

ios - 无法单击 UIView 中的按钮

ios - Xcode 可以对按钮格式差异负责吗?

iphone - 从完成的 ASIHTTPRequest 获取 postValue

objective-c - 不旋转标签栏 View Controller 中的仅一个 View