簡體   English   中英

必須調用超類“UICollectionView”的指定初始值設定項

[英]Must call a designated initializer of the superclass 'UICollectionView'

嘗試初始化集合視圖類並收到以下錯誤:

必須調用超類“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()

}

關於為什么會發生上述情況的任何想法?

您需要調用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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM