Jesse C Jesse C - 4 years ago 264
iOS Question

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?

Answer Source

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)
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download