MikeG
MikeG

Reputation: 4044

UICollectionViewLayout methods never get called

I am working with a UICollectionView and I am trying to use the UICollectionViewLayout methods to adjust the sizing of the cell however they are never being called. The methods are :

sizeForItemAtIndexPath, minimumInterimItemSpacingForSectionAtIndex, and minimumLineSpacingForSectionAtIndex

these 3 methods are never being called and I'm not sure where/ how to call them from. below is my code in Swift 3

import Foundation
import UIKit

class CollectionVC: UICollectionViewController {

    var Sections = [SectionService]()

    override func viewDidLoad() {
        createServiceSections()
    }

    func createServiceSections(){

        ServiceSections.append(ServiceSection(headerTitle: "1", items: 2, headerImage: UIImage(named: "Default"), stats: nil))
        ServiceSections.append(ServiceSection(headerTitle: "2", items: 3, headerImage: UIImage(named: "Default"), stats: nil))
        ServiceSections.append(ServiceSection(headerTitle: "3", items: 3, headerImage: UIImage(named: "Default"), stats: nil))
        ServiceSections.append(ServiceSection(headerTitle: "4", items: 5, headerImage: UIImage(named: "Default"), stats: nil))
    }

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return ServiceSections.count
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ServiceSections[indexPath.section].items.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "servicecell", for: indexPath) as! ServicesCellView

        cell1.serviceLabel.text = "test"
        cell1.serviceImage.image = ServiceSections[indexPath.section].headerImage
        return cell1
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

        print("size for item at indexPath")
        var cellSize: CGSize
        let collectionViewWidth = self.collectionView!.bounds.size.width
        cellSize = CGSize(width: (collectionViewWidth * 0.48), height: (collectionViewWidth * 0.48))
        return cellSize
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInterItemSpacingForSectionAtIndex section: Int) -> CGFloat {
        print("min spacing section")
        return 1
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
        print("min line spacing section")
        return 1
    }
}

Upvotes: 4

Views: 4897

Answers (2)

MikeG
MikeG

Reputation: 4044

With help from Xinatanil, I was able to fix the issue. First, I had to change my class line to this

class CollectionVC: UICollectionViewController, UICollectionViewDelegateFlowLayout {

and then I had to change the methods declarations to Swift 3 syntax...

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    var cellSize: CGSize
    let collectionViewWidth = self.collectionView!.bounds.size.width
    cellSize = CGSize(width: (collectionViewWidth * 0.48), height: (collectionViewWidth * 0.48))
    return cellSize
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 1
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 1
}

Upvotes: 4

Sahil Mahajan
Sahil Mahajan

Reputation: 3990

I think, you haven't set collectionView delegate

Please try:

self.collectionView.delegate = self in viewDidLoad() method

Actually UICollectionViewDelegateFlowLayout inherits from UICollectionViewDelegate. So setting the delegate will be sufficient.

Happy Coding!

Upvotes: 1

Related Questions