Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}
like image 676
Ronald Prithiv Avatar asked Nov 25 '17 07:11

Ronald Prithiv


People also ask

What is swift UICollectionView?

An object that manages an ordered collection of data items and presents them using customizable layouts.

What is Uicollectionviewflowlayout?

A layout object that organizes items into a grid with optional header and footer views for each section.

What is Collectionviewlayout?

An abstract base class for generating layout information for a collection view.


1 Answers

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())
like image 99
Francesco Deliro Avatar answered Oct 23 '22 16:10

Francesco Deliro