错误:必须使用非零布局参数初始化UICollectionView

时间:2017-11-25 07:15:14

标签: 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")
}

}

2 个答案:

答案 0 :(得分:15)

当您展示ChatViewController时,您可能会遇到以下情况:

让chatVC = ChatViewController()

但根据 Apple doc:

类UICollectionViewController

  

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

所以你需要:

 let chatVC = ChatViewController(collectionViewLayout: UICollectionViewFlowLayout())

答案 1 :(得分:-1)

好像你跳过了使用Interface Builder的教程中的步骤。

问题是,你的ChatViewController中你的“collectionView”之间缺少连接吗?和Interface Builder中相应的UICollectionView(所谓的引用插座)。

这是iOS开发中的一项非常基本的任务。请查看“为UI元素创建插座”主题下的官方Apple文档: Connect UI to Code