简体   繁体   中英

CollectionView numberOfItemsInSection variable

I want to have a collectionView inside another collectionView, however the data is coming from an array of arrays so numberOfItemsInSection will be variable based on which parent cell we are populating.

in cellForItemAt indexPath: i am using the following code to extract items from my array:

innerCell.imageCell.file = GlobalParentArray.sharedInstance.globalParentArray[collectionView.tag][indexPath.item].image

which returns the data correctly but only if numberOfItemsInSection is equal to or less than the the count of items at this level of my array:

GlobalParentArray.sharedInstance.globalParentArray[collectionView.tag].count

So i need numberOfItemsInSection to be variable on this count, as this will return the number of items. I haven't had much luck using the .tag property but am searching for a way of having these 2 functions count match up.

this is the actual function I'm using:

   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
    }
}

Currently as this is it throws an error on this line:

fatal error: Index out of range
(lldb)

Well i was on the right track and the solution is something I've never had to use before or even know you could do.

added a conditional to check if the array was empty as it was coming from async call it was behind the numberOfItems function

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
        }
    }
}

apparently this is continuously updated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM