【问题标题】:Error: UICollectionView must be initialized with a non-nil layout parameter错误:UICollectionView 必须使用非零布局参数初始化
【发布时间】:2018-05-09 01:46:46
【问题描述】:

我正在尝试从 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")
}

}

【问题讨论】:

  • 我在 AppDelegate 中遗漏了什么吗?在 AppDelegate 中显示代码。

标签: ios swift swift3 uicollectionview


【解决方案1】:

您似乎跳过了使用 Interface Builder 的教程中的步骤。

问题是,在您的 ChatViewController 中,您的“collectionView”之间缺少连接?以及 Interface Builder 中相应的 UICollectionView(所谓的引用出口)。

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

【讨论】:

  • 我在不使用界面生成器的情况下执行此操作。感谢您分享文档:D
【解决方案2】:

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

让 chatVC = ChatViewController()

但根据 Apple 文档:

类 UICollectionViewController

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

所以你需要:

 let chatVC = ChatViewController(collectionViewLayout: UICollectionViewFlowLayout())

【讨论】:

  • 非常感谢您的宝贵时间和帮助!有效。所以如果我的控制器使用特定的布局,我们必须这样做?
  • 不客气 ;) UICollectionViewController 类需要这种类型的 init。
  • 哦好的 :) 现在我知道了啊
  • 你很棒..爱你
猜你喜欢
  • 2014-08-08
  • 2019-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-19
  • 2017-04-08
  • 1970-01-01
相关资源
最近更新 更多