【问题标题】:CollectionView numberOfItemsInSection variableCollectionView numberOfItemsInSection 变量
【发布时间】:2017-01-26 08:36:11
【问题描述】:

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

【问题讨论】:

    标签: ios arrays swift uicollectionview


    【解决方案1】:

    嗯,我走在正确的轨道上,解决方案是我以前从未使用过的东西,甚至不知道你可以做到。

    添加了一个条件来检查数组是否为空,因为它来自异步调用,它位于 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
            }
        }
    }
    

    显然这是不断更新的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-05
      相关资源
      最近更新 更多