gpt4 book ai didi

ios - 即使提供布局后,UICollectionViewDelegateFlowLayout 也会出错

转载 作者:行者123 更新时间:2023-11-29 05:25:52 25 4
gpt4 key购买 nike

看到以下异常

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

使用这个 ViewController

import UIKit

class SelectDesignController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
private let designCellId = "designCellId"

override func viewDidLoad() {
super.viewDidLoad()
configureNavBar()
configureUI()
collectionView.backgroundColor = #colorLiteral(red: 0.8515156507, green: 0.9521961808, blue: 0.9503603578, alpha: 1)
collectionView.register(DesignCategoryCell.self, forCellWithReuseIdentifier: designCellId)
}
@objc func handleDismiss() {
dismiss(animated: true, completion: nil)
}

func configureNavBar() {
navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage.fontAwesomeIcon(name: .chevronLeft, style: .solid, textColor: .white, size: CGSize(width: 40, height: 40)).withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleDismiss))
}
func configureUI (){
view.backgroundColor = #colorLiteral(red: 0.8515156507, green: 0.9521961808, blue: 0.9503603578, alpha: 1)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: designCellId, for: indexPath) as! DesignCategoryCell
return cell
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
/** PROVIDING a LAYOUT HERE **/
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height)
}

}

我包括 SelectDesignController 如下:

let controller = SelectDesignController()
let nav = UINavigationController(rootViewController: controller)
configureNav(nav: nav, title: "Designs")
present(nav, animated: true, completion: nil)

知道吗,我错过了什么?

最佳答案

您应该检查两件事:

框架

Collection View 的框架矩形,以磅为单位。框架的原点与您计划添加它的 super View 相关。该帧在初始化期间传递给父类(super class)。

布局

用于组织项目的布局对象。 Collection View 存储对指定对象的强引用。不能为零。

关于ios - 即使提供布局后,UICollectionViewDelegateFlowLayout 也会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58178907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com