1

Alright, I am having trouble with a collection view here. This is what Im trying to achieve - 1 big MAIN header and footer, then nested inside there needs to be some number (ex: 4) sections that all have just HEADERS. Like this:


header main

------
tiny header 1
------
[a][b][c]

------
tiny header 2
------
[a][b][c]

footer main

Looking at similar answers I am first trying to just create different subsections since right now I just have all the collection view cells and a big footer and header. Problem is no matter what I have for this, I remain with only 1 SECTION:

 func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 4
    }

I don't know why. I get my collection view from the storyboard and set it up like this:

@IBOutlet var collectionView: UICollectionView!
self.collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) //-44
        self.collectionView.backgroundColor = UIColor.white


func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

        switch kind {

        case UICollectionElementKindSectionHeader:

            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerCell", for: indexPath as IndexPath) as! HeaderCollectionReusableView

           header = headerView
            return headerView

        case UICollectionElementKindSectionFooter:
            let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "footerCell", for: indexPath as IndexPath) as! FooterCollectionReusableView
            //footerView.center = CGPoint(x: footerView.center.x, y: footerView.center.y + 100)

            return footerView

        default:

            assert(false, "Unexpected element kind")
        }
    }

    // ADD STICKER TO CELL
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "stickerCell", for: indexPath as IndexPath) as! StickerCollectionViewCell

        cell.backgroundColor = UIColor.blue

        cell.initSticker(stickerView: stickerPack[indexPath.item])
        cell.stickerName = (stickerPack[indexPath.item].sticker?.localizedDescription)!
        cell.initLabels()

        stickerNames.append(cell.stickerName)
        cell.hasSticker = true;

        //HERE MODIFY SPACING ---------------------------------------
        //then for overflow just increase bottom inset?
        //cell.center = CGPoint(x: cell.center.x, y: cell.center.y+100)

        return cell
    }

How can I do this? Why do I have just 1 section when I have 4 elements to go in the cells, and when I set the "number items in section" to 4 they all show up there?

What am I doing wrong?

blue
  • 7,175
  • 16
  • 81
  • 179

1 Answers1

6

Problem is no matter what I have for this, I remain with only 1 section.

This is because in Swift 3 you need to drop the suffix part of the method name, leaving only numberOfSections, i.e.

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 4
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thank you, but follow up question - now of my array of 4 images, the first one is added as the only item in ALL sections. How can I make it add through the array so image 1 goes in section 1, etc.. – blue Jun 11 '17 at 18:41
  • 1
    @skyguy This is because you are using `indexPath.item` for the index, and ignore `indexPath.section` property. You need to use `section` to decide how to use `item`, depending on the way you store the data for your collection view in your data model. – Sergey Kalinichenko Jun 11 '17 at 18:46
  • 1
    Thank you so much it's work and save my time. @dasblinkenlight – Digvijaysinh Gida Jun 26 '19 at 10:43