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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…