2017-11-25 3 views
0

YouTubeのチュートリアルからFacebookメッセンジャーのようなアプリケーションを作成しようとしています。ユーザーがBarButtonをクリックしてチャットを開くホームページがあります。私は、BarButtonをクリックしてチャットを開くまで、ホームページが正常に動作します。「UICollectionViewは、非nilレイアウトパラメータで初期化する必要があります」というエラーメッセージが表示されてクラッシュします。私はiOSの開発に新しいので、私はすでにinitメソッドを持っているので、問題が正確に何かを理解できません。 。私は異なる見解を持っているので、私はAppDelegateで何かが足りない、これは:(混乱して本当にあなたの助けをお願い申し上げありがとうエラー:UICollectionViewを非ゼロのレイアウトパラメータで初期化する必要があります

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

} 
+0

私はAppDelegateで何か不足していますか? AppDelegateにコードを表示します。 –

答えて

1

あなたChatViewControllerを提示するときおそらくあなたはのようなものがあります。!

LETをchatVC = ChatViewController()

しかしアップルのドキュメントによると:

クラスUICollectionViewController

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

だからあなたが必要とする:あなたがInterface Builderのを使用しているチュートリアルの手順をスキップのよう

let chatVC = ChatViewController(collectionViewLayout: UICollectionViewFlowLayout()) 
+0

お時間をいただきありがとうございました!出来た。私のコントローラが特定のレイアウトを使用している場合、これを行う必要がありますか? –

+0

あなたは歓迎です;)UICollectionViewControllerクラスでは、このタイプのinitが必要です。 –

+0

ああ、今私はああを知っている –

0

は思えます。

問題は、あなたのChatViewControllerには、あなたの "collectionView?" Interface Builderで対応するUICollectionView(いわゆる参照コンセント)を使用します。

これはiOS開発の非常に基本的な作業です。 「UI要素用のアウトレットを作成する」というトピックの公式Appleドキュメントをご覧ください。 Connect UI to Code

+0

私はInterface Builderを使用せずにこれをやっています。ドキュメントをご利用いただきありがとうございます:D –

関連する問題