我想在另一个collectionView中有一个collectionView,但是数据来自数组数组,因此numberOfItemsInSection将根据我们填充的父单元格而变化。
在 cellForItemAt indexPath 中:我使用以下代码从数组中提取项目:
innerCell.imageCell.file = GlobalParentArray.sharedInstance.globalParentArray[collectionView.tag][indexPath.item].image
它会正确返回数据,但前提是 numberOfItemsInSection 等于或小于数组此级别的项目数:
GlobalParentArray.sharedInstance.globalParentArray[collectionView.tag].count
所以我需要 numberOfItemsInSection 在这个计数上是可变的,因为这将返回项目的数量。我在使用 .tag 属性方面运气不佳,但正在寻找一种使这两个函数计数匹配的方法。
这是我正在使用的实际函数:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.outerCollectionView {
return self.packArray.count
} else {
//return self.partArray.count
return GlobalParentArray.sharedInstance.globalParentArray[collectionView.tag].count
}
}
目前,它会在这一行抛出错误:
fatal error: Index out of range
(lldb)
最佳答案
嗯,我走在正确的轨道上,解决方案是我以前从未使用过的,甚至不知道你可以做到。
添加了一个条件来检查数组是否为空,因为它来自异步调用,它位于 numberOfItems 函数后面
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.outerCollectionView {
print(self.packArray.count)
return self.packArray.count
} else {
if GlobalParentArray.sharedInstance.globalParentArray.isEmpty == false {
return GlobalParentArray.sharedInstance.globalParentArray[collectionView.tag].count
} else {
return 0
}
}
}
显然这是不断更新的。
关于ios - CollectionView numberOfItemsInSection 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41869519/