Search code examples
iosswiftsubclasssuperclass

Calling super class into subclass in different swift file


I am having an issue creating a super class and then overriding it in a subclass. I am still somewhat a beginner in iOS and swift so I apologize in advance if my explanations and wording is wrong. See code below I am using:

// created super class in .swiftfile A
class BaseCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    func setupViews() {

    }

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



// swiftfile B trying to call the superclass

class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {


    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = UIColor.rgb(230, green: 32, blue: 31)
        cv.dataSource = self
        cv.delegate = self
        return cv
    }()

    let cellId = "cellId"

    override init(frame: CGRect) {
        super.init(frame: frame)

        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)

        addSubview(collectionView)
        addConstraintsWithFormat("H:|[v0]|", views: collectionView)
        addConstraintsWithFormat("V:|[v0]|", views: collectionView)

    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell (withReuseIdentifier: cellId, for: indexPath)
        // ** when I uncomment this the blue cells show up cell.backgroundColor = UIColor.blue
        return cell

    }

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

    }

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

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

}

//subclass I am trying to call super class into 
class MenuCell: BaseCell {

    override func setupViews() {
        super.setupViews()
        // *** but when I run the app these yellow cells do not show 
        backgroundColor = UIColor.yellow
    }
}

Solution

  • In the cellForItemAt method, you are not casting your cell to your custom class

    guard let cell = collectionView.dequeueReusableCell (withReuseIdentifier: cellId, for: indexPath) as? BaseCell else {
        return UICollectionViewCell()
    }
    

    Hamish also pointed out that you weren't registering your custom cell