CollectionView Index passed resets

off-topic

#1

Hi All,

So I've created a simple collectionView...

Inside each cell of that collectionView is another collectionView that displays the number of cells based on the index of the parent collectionView.

The parent collectionView has 12 cells, representing each month of the year.
The child collectionView should display the number of days in the parent collectionView's month.

For example:

  • Parent collectionView cell index 0 = January , which has 31 days
  • Parent collectionView cell index 1 = February, which has 28 days

*** This logic is already complete in another method***


To simplify and see where I'm going wrong, I've broken it down to just receive 1 cell for each index from the parent collectionView.

Inside the parent collectionView 'cellForItem' method, I'm passing the indexPath.item value to the child variable

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "monthCell", for: indexPath) as! MonthCell

    cell.month = indexPath.item + 1

    return cell
}

/*** Inside the child collectionView

For example:

var month: Int? {
    didSet {

    }
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return month!
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
    
    return cell
}

The issue I'm having, is that it works up until the 3rd index, then it resets back to 1.

So for month 1, I'm getting 1 cell, month 2, I'm getting 2 cells, month 3, I'm getting 3 cells,

  • however when month 4 is shown, it resets back to 1 cell.

Hopefully I'm making sense.

Anyone have a solution or can find my fault.

Thanks


Scroll CollectionView from another CollectionView?
#2

anyone?


(Quinn “The Eskimo!”) #3

anyone?

I think the reason you haven’t got a response is that your question is more closely associated with UICollectionView than the Swift language itself. You might have more luck asking this via a channel dedicated to UIKit, for example, the Cocoa Touch topic area on DevForums.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple