UICollectionView仅允许选择第一个单元格

时间:2016-04-05 00:00:14

标签: ios swift uicollectionview

我使用UICollectionView显示来自外部API的产品。产品在集合视图中完美显示,但由于某种原因,我无法选择任何单元格。我可以选择第一个单元格,但只有当我向上滚动然后快速点击第一个单元格时。我使用故事板创建了一个segue,在控制器中我将数据传递给下一个视图。代码如下:

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 个答案:

答案 0 :(得分:1)

确保您的segue从您的collectionView单元格转到ViewController

检查是否正在调用prepareForSegue,并且当您尝试单击单元格时也会调用didSelectItemAtIndexPath。

同时检查您的collectionView

上是否有任何视图

确保您已选择这些。

View Interaction

相关问题