2017-02-24 47 views
0

我正在使用viewForSupplementaryElementOfKind函數來應用uicollectionview控制器中的標題部分。但是,在對viewDidAppear API進行異步解析之前,行索引會加載到viewForSupplementaryElementOfKind函數中並且超出範圍。我該怎麼辦?uicollectionview控制器中的標題部分。超出範圍

這裏是我的代碼...

override func viewDidAppear(_ animated: Bool) { 
     callVideo3API() 
    } 

    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 

     switch kind { 

     case UICollectionElementKindSectionHeader: 
      let row1 = self.list[0] 
      let row2 = self.list[1] 
      let row3 = self.list[2] 

    let headerSection = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header", for: indexPath) as! HeaderSection 


     headerSection.nameLabel01.text = row1.nickname 
     headerSection.nameLabel02.text = row2.nickname 
     headerSection.nameLabel03.text = row3.nickname 


     return headerSection 

    default: 

     assert(false, "Unexpected element kind") 
    } 
} 
+0

它來了,因爲數據源可能是空的。所以,你必須在獲取數據源後重新加載'collectionView'。併發布你的'numberOfSectionsInCollectionView'和'numberOfItemsInSection'方法。 –

回答

1

您必須等到callVideo3API()得到完成。成功完成callVideo3API()後,您可以重新加載收集視圖以獲取輸出。請按照以下步驟

  1. 調用方法callVideo3API()
  2. CollectionView空通過回零的CollectionView 數據源[func numberOfSections(in collectionView: UICollectionView) -> Intfunc collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int]
  3. (可選)callVideo3API()執行的時間,你可以表演的地方你的CollectionView
  4. callVideo3API()成功完成之後的一個活動的指標,你可以重新加載 CollectionView與相應的數據源的價值。這一次,它 將無故障工作:-) (如果你把活動的指標不要忘了成功的API調用後刪除)