Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
730 views
in Technique[技术] by (71.8m points)

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

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")
}

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Probably when you present your ChatViewController you have something like:

let chatVC = ChatViewController()

But according to Apple doc:

class UICollectionViewController

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

So you need:

 let chatVC = ChatViewController(collectionViewLayout: UICollectionViewFlowLayout())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...