Warve
Warve

Reputation: 511

Must call a designated initializer of the superclass 'UICollectionView'

Trying to initialize a collection view class and getting the following error:

Must call a designated initializer of the superclass 'UICollectionView'

data is being passed from another ViewController and I wish to draw out the frame from within the ViewController when a user searches.

class searchLocationsCollectionViewManager: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate {

weak var collectionView: UICollectionView! {
    didSet {
        collectionView.dataSource = self
        collectionView.delegate = self
    }
}

// Data handler
var data: [[String:Any]]? {
    didSet {
        print(data!)
        //collectionView.reloadData()
    }
}

init(collectionView: UICollectionView, frame: CGRect) {
    self.collectionView = collectionView
    super.init()
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
}



func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

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

// This is the current method for returning the cell.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {




    return UICollectionViewCell()

}

Any ideas on why the above is happening?

Upvotes: 5

Views: 6931

Answers (1)

Nirav D
Nirav D

Reputation: 72410

You need to call designated initializer init(frame:collectionViewLayout:) of UICollectionView.

init(collectionView: UICollectionView, frame: CGRect) {        
    self.collectionView = collectionView
    //Setup collectionView layout here and pass with init 
    let layout = UICollectionViewLayout()
    super.init(frame: frame, collectionViewLayout: layout)
}

Upvotes: 7

Related Questions