簡體   English   中英

“ UICollectionView必須使用非null布局參數初始化”

[英]'UICollectionView must be initialized with a non-nil layout parameter'

我正在嘗試在本教程之后的第一時間創建一個簡單的collectionView

到目前為止,我已經執行了所有步驟,並確保我沒有錯過任何內容。 問題是我收到此錯誤:

由於未捕獲的異常“ NSInvalidArgumentException”而終止應用程序,原因:“ UICollectionView必須使用非空布局參數初始化”

我看過相同問題的不同帖子,但我的答案似乎都沒有解決我的問題。 本教程沒有為collectionView指定初始布局大小,因此我假設Xcode只是自動將其設置為視圖的邊界,因此它沒有非nil布局?

class shootDaysCollectionView: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    private let cellId = "cellId"

    override func viewDidLoad() {

    }

    // VIEW WILL APPEAR
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tabBarController?.navigationItem.title = "SHOOT DAYS"

        collectionView?.backgroundColor = UIColor.white
        collectionView?.register(shootDayCell.self, forCellWithReuseIdentifier: cellId) 
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! shootDayCell

        return cell
    }

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

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: CGFloat(view.frame.width), height: <#T##CGFloat#>)
    }
}

class shootDayCell: UICollectionViewCell {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupViews(){
        backgroundColor = UIColor.red
    }
}

提前非常感謝您!

而不是使用實例化vc

shootDaysCollectionView()

采用

shootDaysCollectionView(collectionViewLayout: UICollectionViewFlowLayout())

如果要配置

let lay = UICollectionViewFlowLayout()

lay...

shootDaysCollectionView(collectionViewLayout:lay)

暫無
暫無

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

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