2016-04-05 90 views
1

我正在使用UICollectionView來顯示來自外部API的產品。產品完美展示在收藏視圖中,但出於某種原因,我無法選擇任何單元格。我可以選擇第一個單元格,但只有當我向上滾動然後快速點擊第一個單元格時。我使用storyboard創建一個segue,並在控制器中將數據傳遞給下一個視圖。代碼如下: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

確保您賽格瑞從您的CollectionView細胞去你的ViewController

檢查被稱爲prepareForSegue,和你的didSelectItemAtIndexPath當您嘗試單擊您的小區也被調用。

還要檢查,如果你有你的CollectionView對頂部的任何意見

確保您已的那些。

View Interaction

+0

的didSelectItemAtIndexPath不會被調用,它顯示了一個打印語句,並沒有被證明的語句。我剛剛證實,收集視圖上方沒有視圖。收集視圖滾動正常,但我無法選擇一個單元格。 –

+0

想通了。我在代碼中爲手勢添加了一個視圖,但沒有考慮到它。謝謝! –

+0

非常好,你想通了 –

相關問題