2

It seems pretty basic but I can't make it work in Swift 4.

So I have the following implementation of UICollectionViewController

class TestController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView?.register(MyCell.self, forCellWithReuseIdentifier: "default")
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        return collectionView.dequeueReusableCell(withReuseIdentifier: "default", for: indexPath)
    }

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


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return .init(width: view.frame.width, height: 100)
    }
}

Although ... numberOfItems ... method is being called, the one for ... cellForItemAt indexPath ... is not.

What am I missing?

This is the code for the cell

class MyCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .green
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

Also, I'm not using storyboards so I'm instantiating this controller as follows in AppDelegate class:

...
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        window = UIWindow()
        window?.makeKeyAndVisible()

        window?.rootViewController = UINavigationController(rootViewController: TestController(collectionViewLayout: .init()))

        return true
    }
...
JaviOverflow
  • 1,434
  • 2
  • 14
  • 31

1 Answers1

2

Instead of

window?.rootViewController = UINavigationController(rootViewController: TestController(collectionViewLayout: .init()))

in didFinishLaunchingWithOptions method use this

 window?.rootViewController = UINavigationController(rootViewController: TestController(collectionViewLayout: UICollectionViewFlowLayout()))
Dipak
  • 2,263
  • 1
  • 21
  • 27
  • one full day lost because of that syntax... what's the difference between using the named constructor and the `.init()`? – JaviOverflow Aug 18 '18 at 22:42
  • Named constructor creates TestController object as well defines layout type. While default constructor only creates object of TestController class. Its basic requirement of collection view to specify Layout type. If you are using default constructor, then you can set layout type in viewDidLoad. e.g. self.collectionView?.collectionViewLayout = UICollectionViewFlowLayout(). If my answer solved your problem plz up vote it... :) – Dipak Aug 19 '18 at 06:58
  • Although your answer helped me to solve my issue, I think the problem relies somewhere else... the parameter collectionViewLayout expects a type `UICollectionViewLayout` so using `.init()` created an instance of that object instead of `UICollectionViewFlowLayout`, which is the one making it work. – JaviOverflow Aug 24 '18 at 17:40