ios - 错误 : UICollectionView must be initialized with a non-nil layout parameter

标签 ios swift swift3 uicollectionview

我正在尝试从 youtube 教程创建一个类似 Facebook Messenger 的应用程序。我有一个主页,用户可以在其中单击 BarButton 打开聊天。主页工作正常,直到我单击 BarButton 打开聊天,它崩溃并显示以下错误消息“必须使用非零布局参数初始化 UICollectionView”。我是 iOS 开发的新手,所以无法真正理解问题出在哪里,因为我已经有了 init 方法。由于我有不同的观点,我是否在 AppDelegate 中遗漏了什么,这令人困惑 :( 。非常感谢您的帮助。谢谢!

class ChatViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.title = "Chats"

    collectionView?.backgroundColor = UIColor.gray

            // Do any additional setup after loading the view.
}

override func viewWillAppear(_ animated: Bool)
{
    collectionView?.register(ChatCell.self, forCellWithReuseIdentifier: "cellID")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

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

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath)

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    return CGSize(width: view.frame.width, height: 100)
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

class ChatCell: UICollectionViewCell
{

override init(frame: CGRect)
{
    super.init(frame: frame)
    setupViews()
}

let profileImageView: UIImageView =
{
    let imageView = UIImageView()
    imageView.contentMode = .scaleAspectFill
    imageView.translatesAutoresizingMaskIntoConstraints = false
    return imageView
}()

func setupViews()
{
    addSubview(profileImageView)

    backgroundColor = UIColor.blue

    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0" : profileImageView]))
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0" : profileImageView]))
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

最佳答案

可能当你展示你的 ChatViewController 时,你有类似的东西:

让 chatVC = ChatViewController()

但根据 Apple 文档

类 UICollectionViewController

When you initialize the controller, using the init(collectionViewLayout:) method, you specify the layout the collection view should have.

所以你需要:

 let chatVC = ChatViewController(collectionViewLayout: UICollectionViewFlowLayout())

关于ios - 错误 : UICollectionView must be initialized with a non-nil layout parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47483529/

相关文章:

swift - 'UIView ?' does not have a member named ' viewWithTag'

ios - 不确定使用 ??在 swift

swift - 类型 'CGRect' 的值没有成员 'makeIntegralInPlace'

ios - 设置 UIScrollView 宽度

ios - 获取对 AWSS3 对象的引用

ios - 如何快速将对象设置为 NSMutableDictionary 中的键

arrays - 快速对数组进行vapor json响应

swift - 检索数据 Firebase Swift 3

iphone - 增强现实 iOS : marker detection and 3D projection

iOS 隔空播放 : my app is only notified of an external display when mirroring is ON?