2016-10-20 8 views
-2

他のすべての質問を実行しようとしましたが、まだエラーが発生します。たぶん私は何かが分からないのですが、ドキュメントを見て、レイアウトを初期化する必要があるようです。UICollectionViewは、ViewDidLoadで初期化した後でも、非nilレイアウトパラメータで初期化する必要があります。

let flowLayout = UICollectionViewFlowLayout() 
flowLayout.scrollDirection = .vertical 
collectionView = UICollectionView(frame: view.frame, collectionViewLayout: flowLayout) 
collectionView?.delegate = self 
collectionView?.dataSource = self 
collectionView?.register(InterestCollectionViewCell.self, forCellWithReuseIdentifier: interestReuseIdentifier) 
view.addSubview(collectionView!) 

コレクションビューのデリゲートメソッド

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return interests.count 
} 

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let vc = collectionView.dequeueReusableCell(withReuseIdentifier: interestReuseIdentifier, for: indexPath) as! InterestCollectionViewCell 
    vc.interestLabel.text = interests[indexPath.row].rawValue as String 
    return vc 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    return CGSize(width: view.frame.size.width, height: 45) 
} 
+2

あなたのコレクションビューを表示するデリゲートメソッド –

+0

デリゲートの問題があるようです。あなたのデリゲートをこのUICollectionViewDelegate、UICollectionViewDataSource、UICollectionViewDelegateFlowLayoutで更新してください。 – Joe

+0

あなたのコードからレイアウト項目サイズがありません.... layout.itemSize = CGSize(width:100、height:100) – Joe

答えて

0

解決策は、私がUICollectionViewControllerを提示する前にcollectionViewを初期化しなければならなかったということでした。そして私はviewDidLoadではなくviewWillAppearに自分のセルクラスを登録しなければなりませんでした。

+4

コードを賢明にしたことを示すことができます –

+4

あなた自身の質問に答えたことは素晴らしいことです。しかし、変更されたコードを表示するように編集してください。 – MwcsMac

関連する問題