ios - CollectionView numberOfItemsInSection 变量

标签 ios arrays swift uicollectionview

我想在另一个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/

相关文章:

ios - 如何使用 UIAlertViewStylePlainTextInput 在 ios 7 中使用 textfieldname.text 和 textfield.placeholder?

c++ - 如何在 C++ while 循环中创建一个自动递增数组?

javascript - 尝试从 componentDidMount 中的 ajax 调用渲染状态值失败(React)

iphone - 在 iPhone 的谷歌地图中添加像 UISlider 或 UIButtom 这样的 subview

ios - Material Design 的 ButtomSheet 无法正确显示 - swift

iphone - 在未知的 NSDictionary/NSArray 深度中搜索对象

java - Printf 不适用于 double 组

ios - iPhone 6+ 上的 SpriteKit 性能不佳

ios - 图像不是从一个 VC 到另一个 VC

ios - 调整图像大小而不扭曲或裁剪它