2016-04-05 19 views
1

私はUICollectionViewを使用して外部APIからの製品を表示しています。製品はコレクションビューに完全に表示されますが、何らかの理由でセルを選択できません。最初のセルを選択することはできますが、スクロールして最初のセルをすばやくタップするだけです。ストーリーボードを使用してセグを作成しています。コントローラでは、データを次のビューに渡しています。以下のコードは次のとおりです。UICollectionViewは最初のセルのみを選択できるようにします

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return self.tableData.count 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell: ProductsViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("productViewCell", forIndexPath: indexPath) as! ProductsViewCell 

    let product = tableData[indexPath.row] 

    cell.configureWithProduct(product, categoryId: "") 

    return cell 
} 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    print("Cell \(indexPath.row) selected") 
} 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 
    if (segue.identifier == "productDetails") { 
     let svc = segue.destinationViewController as! ProductViewController; 

     let indexPaths : NSArray = self.collectionView!.indexPathsForSelectedItems()! 
     let indexPath : NSIndexPath = indexPaths[0] as! NSIndexPath 

     let product = tableData[indexPath.row] 
     svc.productId = product["merged"][0]["id"].int 

    } 
} 

答えて

1

prepareForSegueが呼び出されている、とあなたはあなたのセルをクリックしようとすると、あなたのdidSelectItemAtIndexPathとも呼ばれている場合は必ずあなたのセグエがあなたのViewController

確認するようにcollectionViewセルから行くことを確認します。あなたはcollectionView

の上の任意のビューを持っている場合

もチェックしますが、これらの選択されていることを確認してください。

View Interaction

+0

didSelectItemAtIndexPathが呼び出されていない、それはprint文を示しており、その文が表示されていません。コレクションビューの上にビューがないことを確認しました。コレクションビューはうまくスクロールしますが、セルを選択することはできません。 –

+0

それを考え出した。コード内にジェスチャーのビューを追加し、それを説明しませんでした。ありがとう! –

+0

あなたがそれを理解したことは素晴らしい –

関連する問題