Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift CollectionView不更新可见单元格_Swift_Uicollectionview - Fatal编程技术网

Swift CollectionView不更新可见单元格

Swift CollectionView不更新可见单元格,swift,uicollectionview,Swift,Uicollectionview,下午好 面对这个问题,有一个集合中的单元格出现在整个屏幕上 单元配置了数据, 单元格中有一些事件使集合重新加载, 不可能进行检查,如果单元格现在可见,则不必完全更新,但只需部分数据,我还不知道如何进行检查,是否有人能够提示 测试项目 import UIKit class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { @IBOutlet

下午好

面对这个问题,有一个集合中的单元格出现在整个屏幕上 单元配置了数据, 单元格中有一些事件使集合重新加载, 不可能进行检查,如果单元格现在可见,则不必完全更新,但只需部分数据,我还不知道如何进行检查,是否有人能够提示

测试项目

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    @IBOutlet weak var collectionViwe: UICollectionView!

    let dataSource = [1,2,3,4,5,6,7,8,9]

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionViwe.isPagingEnabled = true
        collectionViwe.register(Cell.self, forCellWithReuseIdentifier: "Cell")
        // Do any additional setup after loading the view.
    }


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! Cell
        let text = String(dataSource[indexPath.row])
        if cell.label.text != text {
            cell.label.text = text
            cell.label.backgroundColor = .random()
        }

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return view.frame.size
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return .zero
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.reloadData()
    }

}

class Cell: UICollectionViewCell {

    let label = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)
        label.backgroundColor = .red
        addSubview(label)
        label.frame = CGRect(x: 36, y: 36, width: 200, height: 100)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}


extension CGFloat {
    static func random() -> CGFloat {
        return CGFloat(arc4random()) / CGFloat(UInt32.max)
    }
}

extension UIColor {
    static func random() -> UIColor {
        return UIColor(red:   .random(),
                       green: .random(),
                       blue:  .random(),
                       alpha: 1.0)
    }
}

没有任何东西可以阻止您更新单元格内的插座以显示新数据。无需重新加载CollectionView或其单元格。我在uicollectionviewcell中有一个uiviewcontroller,如果篮子更改状态为emptytrue/false应在UIViewControllerR中显示篮子指示器,UIViewControllerR将观察者设置为篮子状态没有任何东西可以阻止您更新单元格内的插座以显示新数据。无需重新加载CollectionView或其单元格。我在uicollectionviewcell中有一个uiviewcontroller,如果篮子更改状态为空,则True/false应在UIViewControllerr中显示篮子指示器,UIViewControllerr将观察者设置为篮子状态