错误:必须使用非零布局参数初始化UICollectionView [英] Error: UICollectionView must be initialized with a non-nil layout parameter

查看:111
本文介绍了错误:必须使用非零布局参数初始化UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从youtube教程中创建一个像Facebook应用程序一样的应用程序。我有一个主页,用户单击BarButton打开聊天。主页工作正常,直到我单击BarButton打开聊天,它崩溃时出现以下错误消息UICollectionView必须使用非零布局参数初始化。我是iOS开发的新手,所以无法真正理解问题究竟是什么,因为我已经有了init方法。我在AppDelegate中遗漏了一些东西,因为我有不同的看法,这很令人困惑:(。真的很感谢你的帮助。谢谢!

I am trying to create a Facebook messenger like application from a youtube tutorial. I have a home page where the user click BarButton to open the chats. The Home page works fine, until I click on the BarButton to open the chats, it crashes with the following error message "UICollectionView must be initialized with a non-nil layout parameter". I am new to iOS development so can't really understand what the problem is exactly because I already have the init method. Am I missing something in AppDelegate since I have different views, this is confusing :( . Would really appreciate your help. Thank you!

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时,您可能会遇到以下情况:

Probably when you present your ChatViewController you have something like:

让chatVC = ChatViewController()

let chatVC = ChatViewController()

但根据 Apple doc:

类UICollectionViewController


初始化控制器时,使用init(collectionViewLayout :)方法,指定集合视图应具有的布局。

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

所以你需要:

 let chatVC = ChatViewController(collectionViewLayout: UICollectionViewFlowLayout())

这篇关于错误:必须使用非零布局参数初始化UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆