【发布时间】:2017-02-24 19:43:52
【问题描述】:
我正在使用viewForSupplementaryElementOfKind 函数应用来自uicollectionview 控制器的标题部分。但是,在viewDidAppear API 的异步解析之前,行索引被加载到viewForSupplementaryElementOfKind 函数中并超出范围。我该怎么办?
这是我的代码...
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")
}
}
【问题讨论】:
-
它来是因为数据源可能是空的。因此,您必须在获取数据源后重新加载
collectionView。并发布您的numberOfSectionsInCollectionView和numberOfItemsInSection方法。
标签: ios swift3 uicollectionview uicollectionreusableview