【问题标题】:UICollectionView Only Allows First Cell to be selectedUICollectionView 只允许选择第一个单元格
【发布时间】:2016-04-05 09:53:18
【问题描述】:

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

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

    }
}

【问题讨论】:

    标签: ios swift uicollectionview


    【解决方案1】:

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

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

    还要检查您的 collectionView 上是否有任何视图

    确保您选择了这些。

    View Interaction

    【讨论】:

    • 没有调用 didSelectItemAtIndexPath,它显示了一个打印语句,并且没有显示该语句。我刚刚验证了集合视图上方没有视图。集合视图滚动正常,但我无法选择单元格。
    • 想通了。我在代码中添加了一个手势视图,但没有考虑到它。谢谢!
    • 太棒了,你想通了
    猜你喜欢
    • 2018-08-01
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 2012-12-18
    相关资源
    最近更新 更多