Manuel
Manuel

Reputation: 23

UICollectionView - Why are the methods not called?

I call out of another collection view cell a new collection view:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let rezeptLauncher = RezeptLauncher()
    rezeptLauncher.ShowRezept()
}

Afterwards it shows the collection view, but no cells inside.

I read almost every page about this topic. Nothing is working. Any suggestions? Thank you so much in advance!

class RezeptLauncher: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

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

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        cell.backgroundColor = .red
        return cell
    }

    var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.translatesAutoresizingMaskIntoConstraints = false
        cv.backgroundColor = .white
        cv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        return cv
    }()

    func ShowRezept() {
        print("Rezept wird angezeigt")
       if let keyWindow = UIApplication.shared.keyWindow {
        keyWindow.addSubview(collectionView)
        collectionView.backgroundColor = .white
        collectionView.topAnchor.constraint(equalTo: keyWindow.topAnchor, constant: 40).isActive = true
        collectionView.leadingAnchor.constraint(equalTo: keyWindow.leadingAnchor, constant: 40).isActive = true
        collectionView.trailingAnchor.constraint(equalTo: keyWindow.trailingAnchor, constant: -40).isActive = true
        collectionView.bottomAnchor.constraint(equalTo: keyWindow.bottomAnchor, constant: -40).isActive = true
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.reloadData()
        }

    }
}

Upvotes: 2

Views: 868

Answers (1)

Eyzuky
Eyzuky

Reputation: 1935

It seems like your Rezept object is being deallocated by Swift as its lifecycle is inside your did select function.

If it is deallocated, the collectionView has no delegate anymore. Since delegates are weak entities by best practice, they will not be held strongly (thus deallocation removes it)

You should add it as an instance of the class that holds the outer collectionView, and only call the setup function inside didSelect.

Upvotes: 2

Related Questions