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
}
}
的didSelectItemAtIndexPath不會被調用,它顯示了一個打印語句,並沒有被證明的語句。我剛剛證實,收集視圖上方沒有視圖。收集視圖滾動正常,但我無法選擇一個單元格。 –
想通了。我在代碼中爲手勢添加了一個視圖,但沒有考慮到它。謝謝! –
非常好,你想通了 –