2017-07-19 11 views
0

ボタンのコレクションビューを使用しようとしています。ストーリーボードでは、8つのボタンをクリックしてコレクションビューに追加しました。コレクションビューには、ボタンごとに8個の固有識別子が付いた8個のセルがあります。すべてのボタンは、コレクションビューに追加したときに表示されます。ボタンをCollectionView(Xcode 8)にドラッグ

しかし私の問題は、私がビルドして実行すると、セルの8つのボタンが全く表示されないというエラーは発生しないということです。

私はバックグラウンドでコードを実行しませんでした。ドラッグ&ドロップして属性を使用するだけです。

だから私はなぜそれが表示されないのか混乱しています。私はいくつかの指導に感謝します。

+0

デリゲートとデータソースを設定するには、コレクションビューからvcにドラッグを制御しましたか?あなたのコレクションビューデリゲートメソッドにも従ってください。 – Douglas

+0

@Douglas:はい、しました。私はもう少し研究をしました。私はそれをすることを忘れてしまったので、ありがとう!しかし、私は異なる識別子を持つ複数のセルを持っています(私は1つの識別子を使うことはできません)。 – insolence

+0

cellforindexpathメソッドでは、おそらく1つの再利用識別子を指定しました。 8種類の細胞タイプがある場合は、順序を把握してifステートメントを使用する必要があります。インデックスパスが1の場合、識別子「firstcell」のセルを使用します。インデックスパスが2の場合は、別のパスを使用します。 – Douglas

答えて

1

質問に答えるために@ Douglasのコメントを使用しましょう。だから私は完全にやるのを忘れて最初にすることは、ストーリーボードでのViewControllerCTRL +ドラッグコレクションビューにしたとのdataSourceデリゲートを有効にします。次に私のViewController.swiftで私は次のコードを実装しました:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    if(indexPath.item == 0) { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "kitchenButton", for: indexPath) 
     return cell 
    } else if(indexPath.item == 1) { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cameraButton", for: indexPath) 
     return cell 
    } else if(indexPath.item == 2) { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "eventsButton", for: indexPath) 
     return cell 
    } else if(indexPath.item == 3) { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mapButton", for: indexPath) 
     return cell 
    } else if(indexPath.item == 4) { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "journeyButton", for: indexPath) 
     return cell 
    } else if(indexPath.item == 5) { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "callButton", for: indexPath) 
     return cell 
    } else if(indexPath.item == 6) { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "handsButton", for: indexPath) 
     return cell 
    } else if(indexPath.item == 7) { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "swimButton", for: indexPath) 
     return cell 
    } else { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "none", for: indexPath) 
      return cell 
    } 


} 

次のコードは複数のセル用です。私はこのメソッドが私のために美しく働くように水平スクロール効果を達成しようとしています。

クラスのヘッダーには、UICollectionViewDelegateFlowLayout、UICollectionViewDataSourceを必ず含めてください。

ありがとう@ダグラス!

+0

行きたい!あなたがそれが働いていることを聞いてうれしい! – Douglas