不工作重装数据CollectionView

时间:2017-08-26 20:45:22

标签: ios swift collectionview

我在ViewController1中有一个集合,如果我点击它转到ViewController2我可以更改图像;当我按下导航控制器上的按钮返回ViewController1时,我应该看到我在ViewController2中更改的图像。我的问题是我需要重新加载CollectionView的数据,但我不能这样做!我已经尝试将CollectionView.reloaddata()放入**ViewWillAppear**,但什么都没发生!我怎么能这样做?

import UIKit

private let reuseIdentifier = "Cell2"

class CollectionViewControllerStatiVegetarian: UICollectionViewController {

    let baza1 = Baza()

    @IBOutlet var CollectionViewOut: UICollectionView!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        CollectionViewOut.reloadData()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        let backround = CAGradientLayer().turquoiseColor()
        backround.frame = self.view.bounds
        self.collectionView?.backgroundView = UIView()
        self.collectionView?.backgroundView?.layer.insertSublayer(backround, at: 0) 
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        let size2 = baza1.superTuples(name: "2")
        let x = Mirror(reflecting: size2).children.count  // 
        return Int(x+1)
    }

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

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as! CollectionViewCell_VegetarianStaty
        if indexPath.row != 0 {
            cell.shapka_stati_vegetarian.image = nil
            let superTupl = baza1.superTuplesShapka(Nomer_tupl: (indexPath.row-1))
            cell.label.text = superTupl.5
            let tupl = baza1.superTuplesShapka(Nomer_tupl: (indexPath.row-1))
                if (tupl.2 == 1) {
                    cell.shapka_stati_vegetarian.image = nil
                    cell.shapka_stati_vegetarian.image = UIImage(named: "fon_galochka.png")
                } else {}
        } else {
            cell.shapka_stati_vegetarian.image = UIImage(named: "shapkastaty")
            cell.label.text = ""
        }
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath  indexPath: NSIndexPath) -> CGSize  {

        let screenWidth  = UIScreen.main.fixedCoordinateSpace.bounds.width
        let height = screenWidth*550/900+20
        var size = CGSize(width: screenWidth, height: 73)
        if indexPath.row==0 {
            size = CGSize(width: screenWidth, height: height)
        }
        return size
    }

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.row != 0 {
            numb_cell = indexPath.row
            let bazaSh = Baza()
            let f = bazaSh.superTuplesShapka(Nomer_tupl: (indexPath.row-1) )
            let vc = storyboard?.instantiateViewController(withIdentifier: "ViewStaty")  as! ViewController
            vc.obr = f.3
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}

3 个答案:

答案 0 :(得分:1)

视图仅在视图控制器的生命周期中加载一次,因此viewDidLoad仅运行一次。

执行此操作的一种方法是重新加载viewWillAppear中的数据,该数据在视图显示时触发,但这可能会多次运行。

另一种方法是使用由vc1实现的vc2的委托方法。当在vc2中更改数据时运行此委托方法,并且由于vc1实现了委托,因此可以选择重新加载视图。

另一种方式,也就是我喜欢的方式,是使用像Core Data这样的模型。这样当vc2更改数据时,vc1可以观察它感兴趣的对象的状态,并通过NSFetchedResultsControllerDelegate方法对模型中的更改做出反应。

您可以选择使用Realm作为持久性机制,我确信有类似的方法来观察模型并对变化做出反应。

答案 1 :(得分:0)

inn为了在后台线程中重新加载数据,您需要使用

DispatchQueue.main.async { self.collectionView.reloadData() }

答案 2 :(得分:0)

通过

实现您的vc
UICollectionViewDelegate , UICollectionViewDataSource 

然后 viewDidLoad()

self.collectionView.delegate = self
self.collectionView.dataSource = self