-1

I want to set UICollectionViewLayout for my UICollectionView but when the layer is set, the function cellForItemAt doesn't called. I don't understand why. My UICollectionView is in a XIB, with this code for testing the UICollectionViewLayout :

required init?(coder aDecoder: NSCoder)
{
    super.init(coder: aDecoder)
    xibSetup()

    self.collectionView.register(UINib(nibName: "StepCell", bundle: nil), forCellWithReuseIdentifier: stepCellIdentifier)
    self.collectionView.delegate = self
    self.collectionView.dataSource = self
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 3
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if let lCell = collectionView.dequeueReusableCell(withReuseIdentifier: stepCellIdentifier, for: indexPath) as? StepCell {
        lCell.text.text = indexPath.row.description
        return lCell
    }
    return UICollectionViewCell()
}

I have a custom UICollectionViewCell in another xib and this is my UICollectionViewLayout class :

class DetailGroupCollectionviewLayout: UICollectionViewLayout {
    let CELL_HEIGHT = 30.0
    let CELL_WIDTH = 100.0

    var cellAttrsDictionary = Dictionary<IndexPath,UICollectionViewLayoutAttributes>()
    var contentSize = CGSize.zero

    override var collectionViewContentSize: CGSize {
        return self.contentSize
    }

    override func prepare() {
        if let lCollectionView = self.collectionView {
            if lCollectionView.numberOfSections > 0 {
                for section in 0...lCollectionView.numberOfSections - 1 {
                    if lCollectionView.numberOfItems(inSection: section) > 0 {
                        for item in 0...lCollectionView.numberOfItems(inSection: section) - 1 {
                            let cellIndex = IndexPath(item: item, section: section)
                            let xPos = Double(item) * CELL_WIDTH
                            let yPos = Double(section) * CELL_HEIGHT

                            let cellAttributes = UICollectionViewLayoutAttributes(forCellWith: cellIndex)
                            cellAttributes.frame = CGRect(x: xPos, y: yPos, width: CELL_WIDTH, height: CELL_HEIGHT)
                            cellAttributes.zIndex = 1

                            cellAttrsDictionary[cellIndex] = cellAttributes
                        }
                    }
                }
            }
        }
    }
}

I have tried to have everything about the UICollectionView in storyboard because i was thinking that a xib "bug" but this resolve nothing.

There is a thing that i don't make correctly or a particularity ? I'm lost.

I'm on Xcode 8.3 with Swift 3.1 and my app is on iOS 10.

Jordan Favray
  • 179
  • 1
  • 12

4 Answers4

0

Are you sure your ReuseIdentifier is correct? Have you tried if it gets inside the if let here? (with a print statement or breakpoint)?

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if let lCell = collectionView.dequeueReusableCell(withReuseIdentifier: stepCellIdentifier, for: indexPath) as? StepCell {
        lCell.text.text = indexPath.row.description
        return lCell
    }
    return UICollectionViewCell()
}

Not really sure what it could be, if you can share your project, or a part of it, I'd gladly try to figure it out with you. Also, make sure you conform to UICollectionViewDataSource and UICollectionViewDelegate.

JoniVR
  • 1,839
  • 1
  • 22
  • 36
0

Did you attribute the delegate to the viewController? That could be one possibility. (If you haven't, the "numberOsSections" won't get called either.

0

The ReuseIdentifier is correct. The trouble is, the cellForItemAt don't called just when i add the UICollectionViewLayout, when is not set, the UICollectionView run perfectly.

Without UICollectionViewLayout

With UICollectionViewLayout

Something I forgot to tell you, It does not matter when UICollectionViewLayout is set or not, my xibController go into "numberOfItemsInSection" and "numberOfSections" bu only when the UICollectionViewLayout is set, the controller not pass into the cellForItemAt

Jordan Favray
  • 179
  • 1
  • 12
0

I find the solution, my custom UICollectionViewLayout has no contentSize, I added this in my prepare() function :

let contentWidth = Double(collectionView!.numberOfSections) * CELL_WIDTH
let contentHeight = Double(collectionView!.numberOfItems(inSection: 0)) * CELL_HEIGHT
self.contentSize = CGSize(width: contentWidth, height: contentHeight)

This is the result

Jordan Favray
  • 179
  • 1
  • 12