2017-02-24 48 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调用后删除)