2017-08-26 99 views
0

我有一個集合中的ViewController1,如果我點擊它進入ViewController2在那裏我可以改變圖像;當我按下導航控制器上的按鈕返回ViewController1時,我應該看到我在ViewController2中更改的圖像。我的問題是我需要重新加載CollectionView的數據,但我做不到!我已經嘗試將CollectionView.reloaddata()放入**ViewWillAppear**,但沒有發生任何事情!我怎樣才能做到這一點?不工作重裝數據的CollectionView

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) 
     } 
    } 
} 
+0

從'ViewController2'添加代碼,您在其中更改'UICollectionView'的數據。 – JuicyFruit

+0

如果您已經使用一個對象數組作爲數據源,則不需要委託。將對象傳遞給vc2(供參考)更改圖像,當它返回時,以viewwillappear重新加載它。 –

回答

1

視圖在視圖控制器的生命週期中只加載一次,所以viewDidLoad只能運行一次。要做到這一點

一種方法是重新加載viewWillAppear視圖出現時被解僱的數據,但這可能會遇到很多次。

另一種方式是具有由VC1實現VC2的委託方法。此代理方法在vc2中更改數據時運行,並且由於vc1實現代理,因此可以選擇重新加載視圖。

的另一種方式,和一個我喜歡,就是使用類似的核心數據的一種模式。當VC2更改數據的方式,VC1可觀察對象的狀態是興趣和反應通過NSFetchedResultsControllerDelegate方法模型中的更改。

你可以選擇使用領域作爲持久性機制,而且我敢肯定有一個類似的方式來觀察模型和對變化做出反應。

相關問題