Taylor Glaeser
Taylor Glaeser

Reputation: 1313

Separate UICollectionView dataSource and delegate not being called

I'm attempting to use a separate class for my UICollectionView DataSource and Delegate, but the methods are not being called.

The view hierarchy is as follows:

(Root VC)        -> (Current Screen) -> (Current Page)       -> (CollectionView in question)
UIViewController -> UIViewController -> UIPageViewController -> UICollectionView

This is the class for the Current Screen VC.

import UIKit

class FooController: UIViewController {
  // PageViewController
  let pageController = UIPageViewController(/*...*/)
  let pageOne = UIViewController()
  let pageTwo = UIViewController()

  override func loadView() {
    super.loadView()
    // Setup Stuff
    bootstrapPageOne()
  }

  func bootstrapPageOne() {
    // Do PageView Stuff
    bootstrapBars()
  }

  func bootstrapBars() {
    // Everything is set up when this function is called.

    let collectionViewFlowLayout = UICollectionViewFlowLayout()
    let cellSize : CGFloat = 60

    let collectionViewWidth = view.bounds.width - 20 * 2 //CGFloat(barsCount.count) * cellSize
    let collectionViewHeight = cellSize
    let collectionViewFrame : CGRect = CGRect(x: 20, y: view.bounds.height - (60 + view.layer.cornerRadius), width: collectionViewWidth, height: collectionViewHeight)

    let barCollectionView = UICollectionView(frame: collectionViewFrame, collectionViewLayout: collectionViewFlowLayout)
    let fooBarHandler = FooBarCollectionHandler()
    barCollectionView.delegate = fooBarHandler
    barCollectionView.dataSource = fooBarHandler
    barCollectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "barCellIdentifier")
    barCollectionView.backgroundColor = UIColor.blueColor()

    pageOne.view.addSubview(barCollectionView)
  }
}

This is the class for the CollectionView DataSource and Delegate

import UIKit

class FooBarCollectionHandler: NSObject, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
  let settings = SettingsHandler.sharedSettings

  func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    debugPrint(#function)
    return 1
  }

  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    debugPrint(#function)
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("barCellIdentifier", forIndexPath: indexPath)

    return cell
  }

  func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    debugPrint(#function)
    return settings.bars.count // 5
  }

  func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    debugPrint(#function)
    return CGSizeMake(40,40)
  }
}

// MARK: - Touch handling
extension FooBarCollectionHandler {
  func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    debugPrint(#function)
    debugPrint(indexPath.row)
  }
}

With the current configuration, none of the delegate/datasource methods are called. I can call barCollectionView.numberOfSections() or barCollectionView.numberOfItemsInSection(_:) and it will call and return, but they are not called automatically.

NOTE: The UICollectionView renders fine, but the cells do not.

Upvotes: 3

Views: 3128

Answers (1)

CZ54
CZ54

Reputation: 5588

The foobarhandler has a weak reference as Delegate. So when you leave the scope of bootstrapBars, it is released.

Store it locally to keep a reference :

var myDelegate : FooBarCollectionHandler?

func bootstrapBars() {
    //your stuff
    myDelegate = FooBarCollectionHandler()
    barCollectionView.delegate = myDelegate
    barCollectionView.dataSource = myDelegate

}

Upvotes: 10

Related Questions