2017-09-19 46 views
0

我有兩個問題。uicollectionview - 無需調用reloaddata自動加載數據

  1. 我很奇怪,爲什麼我收集的看法自動加載數據,而無需調用imageCollectionView.reloadData()

    已解決。見評論

  2. 爲什麼func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize不叫?我沒有看到print("collectionViewLayout called")我試圖修改單元格的大小,因此單元格的高度等於集合視圖高度

    已解決。見註釋

這裏是代碼

class ProductInternalDetailVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 

    var selectedProduct: Product? 
    @IBOutlet weak var imageCollectionView: UICollectionView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     imageCollectionView.delegate = self 
     imageCollectionView.dataSource = self 
    } 

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath) as! ProductInternalDetailCVC 
     if indexPath.row == 0 { 
      cell.productImage.image = selectedProduct!.image1 
     } else { 
      cell.productImage.image = selectedProduct!.image2 
     } 
     cell.productImage.frame = CGRect(x: 1, y: 1, width: cell.frame.size.width-2, height: cell.frame.size.height-2) 
     return cell 
    } 

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
     //return CGSize(width: collectionView.frame.size.height-1, height: collectionView.frame.size.height-1) 
     print("collectionViewLayout called") 
     return CGSize(width: 10, height: 10) 
    } 
} 

class ProductInternalDetailCVC: UICollectionViewCell { 
    @IBOutlet weak var productImage: UIImageView! 
} 

感謝您的幫助。

+3

'FUNC的CollectionView(的CollectionView:UICollectionView,佈局collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) - > CGSize'是從一個在doc不同:'FUNC的CollectionView(_的CollectionView:UICollectionView,佈局collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath :IndexPath) - > CGSize'(它缺少像其他的'_'。 – Larme

+0

CollectionView首次自動加載,'viewDidLoad'後,我希望你知道:) –

+1

哦,謝謝你。所有的問題解決。 –

回答

3

問題2:函數簽名發生了一些變化。用此更改函數簽名:

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

並且不要忘記實施UICollectionViewDelegateFlowLayout協議。

問題1:

當進入的ViewController您的CollectionView加載的CollectionView的數據源自動如果這是你的question.for例如,就數據源的變化不改變視圖(指沒有調用viewDidLoad方法),你會直到您撥打imageCollectionView.reloadData()才能看到更改。

+0

你好,@kingalione你能幫我解決,我有同樣的問題,我陷入這種情況。 –

+0

問題是什麼? – Kingalione

相關問題