UICollectionViewDelegateFlowLayout methods not called in Swift 2.3 App
Asked Answered
C

3

8

None of my UICollectionViewDelegateFlowLayout methods are called. Can somebody help me find a solution?

In my ViewDidLoad method, I have set delegate and datasource for collection view as self.collectionView.delegate = self and self.collectionView.dataSource = self

Here is my delegate methods:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    let deviceIdiom = UIScreen.mainScreen().traitCollection.userInterfaceIdiom
    switch(deviceIdiom){
    case .Phone:

        return UIEdgeInsets(top: 50, left: 10, bottom: 0, right: 10)
    case .Pad:
        return UIEdgeInsets(top: 60, left: 20, bottom: 0, right: 20)
    default:
        break
    }
}

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

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

    let deviceIdiom =          UIScreen.mainScreen().traitCollection.userInterfaceIdiom

    switch(deviceIdiom){
    case .Phone:

        let cellWidth = (view.bounds.size.width - 16) / 2
        let cellHeight = cellWidth
     return CGSize(width: cellWidth, height: cellHeight)

    case .Pad:

        let cellWidth = (view.bounds.size.width - 40) / 2
        let cellHeight = cellWidth
        return CGSize(width: cellWidth, height: cellHeight)

    default:
        break
    }
}

My ViewDidLoad method-

override func viewDidLoad() { super.viewDidLoad()

 self.collectionView.dataSource = self
 self.collectionView.delegate = self
    cellImages = [
        "contact.png",
        "share.png",
        "setting.png",
        "logout.png"]
    cellLabels = [
        "Contact",
        "Share",
       "Setting",
        "Logout"]

    self.view.backgroundColor = uicolorFromHex(0xE5DADA)

    self.collectionView?.backgroundColor = uicolorFromHex(0xE5DADA)
    self.navigationController?.navigationBarHidden = false
    self.navigationItem.hidesBackButton = true
    navigationController!.navigationBar.titleTextAttributes =
        [NSForegroundColorAttributeName: UIColor.whiteColor()]
    navigationController!.navigationBar.barTintColor = UIColor(red: 0.0431, green: 0.0824, blue: 0.4392, alpha: 1.0)

    if let user = User.currentUser(){
    if(user.manager){
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Board", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(goToBoard))

}

Thanks,

Clink answered 29/1, 2017 at 11:23 Comment(4)
Have you set breakpoints and made sure that they are not called? Unless you did that, then it is possible that your switch is just falling through to the default case.Ribeiro
post whole viewDidLoad methodRoyalty
@BenjaminLowry Yes, I did set breakpoints. Thanks,Clink
@Royalty ViewDidLoad added. ThanksClink
C
7

I finally solved this by creating extension of my class MainViewController : UICollectionViewDelegateFlowLayout { } , putting all delegate method in there and cleaning build. It works property in Swift 3 (in the same class) but for swift 2.3 for some strange reason it needs to be put in extension. Thanks,

Clink answered 29/1, 2017 at 23:34 Comment(0)
Q
4

For anyone coming here, another reason for the case might be not setting the delegate. Just check that you assign the collectionView delegate to self.

collectionView.delegate = self
Quid answered 2/10, 2018 at 10:34 Comment(0)
P
1

I managed to fix the same issue by calling

collectionView.delegate = self

BEFORE

collectionView.dataSource = self

(I have static items and never reload the data)

Padus answered 4/6, 2021 at 8:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.