尝试初始化集合视图类并收到以下错误:
必须调用超类'UICollectionView'
的指定初始值设定项
正在从另一个ViewController传递数据,我希望在用户搜索时从ViewController中抽出帧。
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()
}
关于上述原因发生的任何想法?
答案 0 :(得分:5)
您需要调用UICollectionView
的指定初始值设定项init(frame:collectionViewLayout:)
。
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)
}