【问题标题】:collectionView :didSelectItemAtIndexPath called with a delaycollectionView :didSelectItemAtIndexPath 延迟调用
【发布时间】:2018-04-11 05:06:21
【问题描述】:

我有一个加载产品列表的集合视图,其中没有花哨的功能。它只是从 API 加载数据。问题是当我尝试点击集合视图中的一项时

collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)

没有被调用。但是当我长按一个单元格时,它会被调用。

我很确定单元格上没有应用手势。

谁能给我一些指导来解决这个问题。

我的代码库如下

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let cellTapped     =   (self.storeSearchListingCollectionViewOutlet.cellForItem(at: indexPath)) as! StoreSearchProdListingCollectionViewCell

        UStoreShareClass.storeSharedInstance.longTappedProductID = String(cellTapped.tag)

        if((UStoreShareClass.storeSharedInstance.longTappedProductID) != nil)
        {
            self.performSegue(withIdentifier: "searchToProductDetailSegue", sender: self)
        }
        else
        {
            SetDefaultWrappers().showAlert(info:SERVER_DOWN_ERROR_ALERT, viewController: self)
        }
}

View Hierarchy如下

在此先感谢...!!!

【问题讨论】:

  • 您的视图层次结构是什么? CollectionView 是在 ScrollView 内吗?或其他有自己检测的子视图
  • 最好使用视图调试并检查collectionviewCell的contententView之上是否有任何视图。确保您在 mainThread 上重新加载 collectionView
  • @iOSGeek 添加了有问题的视图层次结构屏幕
  • @Pandey_Laxman Didselect 委托函数在我按下单元格 3/4 秒时被调用...
  • @JobinsJohn 在 View Debugging 中捕获模拟器或设备上的运行屏幕,然后检查。

标签: ios swift uicollectionview swift4


【解决方案1】:

这是故意的。在 Storyboard 上,转到 Collection View 并取消选中属性检查器中的 "Delay Touch Down"

【讨论】:

    【解决方案2】:

    可能有两个原因:

    1) 第一个原因可能是:

    很遗憾,您的代码没有让主线程在 UI 上运行。所以你必须像这样把你的 UI 相关部分放在主线程中。

    DispatchQueue.main.async {
    // PUT YOUR CODE HERE
                if((UStoreShareClass.storeSharedInstance.longTappedProductID) != nil)
                {
                    self.performSegue(withIdentifier: "searchToProductDetailSegue", sender: self)
                }
                else
                {
                    SetDefaultWrappers().showAlert(info:SERVER_DOWN_ERROR_ALERT, viewController: self)
                }
        }
    

    2) 第二个原因:

    您是否关闭了collectionView 弹跳功能? 在 Storyboard 中,选中 collectionView 的“滚动时反弹”复选框(如果未选中)。

    【讨论】:

      【解决方案3】:

      对于这个老问题的未来读者 - 将来可能是我 ;)

      我有一个 UITapGestureRecognizer 正在吞下我的短水龙头(在 UICollectionView 的祖父母中),因此只有旧的水龙头才能通过!我估计你也有同样的问题。您只需添加tapRecognizer.cancelsTouchesInView = false

      let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapDidTouch(sender:)))
      tapRecognizer.cancelsTouchesInView = false
      scrollView.addGestureRecognizer(tapRecognizer)
      

      【讨论】:

        【解决方案4】:

        首先,在 if 条件中放置断点,并检查它是否进入了 didselect 块。那么,

        尝试在主线程中添加您的“performsegue”代码,如下所示

        if((UStoreShareClass.storeSharedInstance.longTappedProductID) != nil)
        {
        
         DispatchQueue.main.async 
        { 
         self.performSegue(withIdentifier: "searchToProductDetailSegue", sender: self)
        
        }
        }
        else
        {
            SetDefaultWrappers().showAlert(info:SERVER_DOWN_ERROR_ALERT, viewController: self)
        }
        

        【讨论】:

        • 它没有到达 didSelct 委托方法
        猜你喜欢
        • 1970-01-01
        • 2018-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多