2016-08-30 24 views
0

特定のタスクを実行するために、コレクションビューセルが選択されている部分をどのように処理すればよいですか?ここで私のコードですが、動作していません、助けてください。1つのコントローラ内の2つのコレクションビューセル

func collectionView(collectionView: UICollectionView!, didSelectItemAtIndexPath indexPath: NSIndexPath) { 

    if let cell = latestNewsCollectionView.cellForItemAtIndexPath(indexPath){ 
     print("latest selected")  
    } 
    if let cell = promotionCollectionView.cellForItemAtIndexPath(indexPath){ 
     print("promotion selected") 
    } 

私が手出力は次のようになります。最後に

latest selected 

promotion selected 

すべてのコレクションビューのセルが選択され、どのように私はこの問題を解決することができますか?

+0

あなたは約2 'collectionViewCell'を取った後、2' collectionView'を比較していますか? –

答えて

1

cellForRowAtIndexPathdidSelectRowAtIndexPathの中にチェックを入れてください。 しかし、コレクションビューを宣言し、そこにIBOutletsを接続してください。次に、このようにチェック

@IBOutlet var collectionView1:UICollectionView! 
@IBOutlet var collectionView2:UICollectionView! 

を置く:

func collectionView(collectionView: UICollectionView!, didSelectItemAtIndexPath indexPath: NSIndexPath) { 


    if let collectionView == collectionView1 { 

     print("latest selected") 

    } 

    if let collectionView == collectionView2 { 

     print("promotion selected") 

    } 
} 
関連する問題