Ron*_*hiv 5 ios uicollectionview swift swift3
我试图从youtube教程创建一个像Facebook应用程序的应用程序.我有一个主页,用户单击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")
}
}
Run Code Online (Sandbox Code Playgroud)
Fra*_*iro 21
可能当您展示ChatViewController时,您可能会遇到以下情况:
让chatVC = ChatViewController()
但根据Apple doc:
class UICollectionViewController
初始化控制器时,使用init(collectionViewLayout :)方法指定集合视图应具有的布局.
所以你需要:
let chatVC = ChatViewController(collectionViewLayout: UICollectionViewFlowLayout())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4515 次 |
| 最近记录: |