5

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?

1
  • What is the purpose of an UICollectionView property in an UICollectionView subclass? Consider that didSet is not called when the initial value is assigned. And in case you mean UICollectionViewController there is already an UICollectionView property. Commented Feb 2, 2017 at 10:39

1 Answer 1

8

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)
}
Sign up to request clarification or add additional context in comments.

4 Comments

My class is extending from UICollectionView and it doesn't have collectionView property saying: Value of type 'MyCollectionView' has no member 'collectionView'
Why this is only for UICollectionViewController?
Why UICollectionView doesn't have collectionView
@Daniella because UICollectionView don't have collectionView property you need to create object of your MyCollectionView which extends UICollectionView.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.