简体   繁体   English

UICollectionView didSelectItemAt 未被调用

[英]UICollectionView didSelectItemAt not being called

I am puzzled because It is just a simple thing but doesn't work.我很困惑,因为这只是一件简单的事情,但不起作用。

This is my checked list before write question at here.这是我在这里写问题之前的检查清单。

  1. UICollectionViewDelegate is registered UICollectionViewDelegate 已注册

  2. There is nothing on above than UICollectionView from "Debug View Hierarchy" “Debug View Hierarchy”中的 UICollectionView 以上没有任何内容

  3. isUserInteractionEnabled of UICollectionView and UICollectionViewCell is true UICollectionView 和 UICollectionViewCell 的isUserInteractionEnabledtrue

  4. isAllowsSelection of UICollectionView is true isAllowsSelection UICollectionView 为true

Is there anything else I should check out?还有什么我应该检查的吗?

===== =====

class MyView: NibDesignable { // It's UIView with NibDesignableProtocol (https://github.com/mbogh/NibDesignable)
    @IBOutlet weak var collectionView: UICollectionView!

    override init(frame: CGRect) {
        super.init(frame: frame)

        self.setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.setup()
    }

    private func setup() {
        print("test - It's called.")

        self.collectionView.dataSource = self
        self.collectionView.delegate = self
        self.collectionView.isUserInteractionEnabled = true
        self.collectionView.isAllowsSelection = true
    }
}

extension MyView: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath {
        print("test - It's not called")
    }
}

I pasted only the part relating to question because all of code is too long.我只粘贴了与问题相关的部分,因为所有代码都太长了。

This can happen if you have a gestureRecognizer in your custom cell.如果您的自定义单元格中有一个gestureRecognizer,就会发生这种情况。 this will override the didSelectItemAt delegate of the collectionView.这将覆盖 collectionView 的 didSelectItemAt 委托。

If this is your issue, then you can resolve it by identifying the gestureRecognizer and doing the following:如果这是您的问题,那么您可以通过识别 gestureRecognizer 并执行以下操作来解决它:

let tap = UITapGestureRecognizer(target: self, action: #selector(self.someViewInMyCellTapped(_:)))
someViewInMyCell.addGestureRecognizer(tap)

tap.cancelsTouchesInView = false

isUserInteractionEnabledMyView本身或其任何父项上可能为 false。

If anyone's experiencing same issue as me with Simulator XR try out other simulators.如果有人在使用 Simulator XR 时遇到与我相同的问题,请尝试使用其他模拟器。 My didSelectItemAt method was never being called for some indexes when using XR simulator and index were random everytime I relaunched the simulator.使用 XR 模拟器时,我的 didSelectItemAt 方法从未被调用用于某些索引,并且每次我重新启动模拟器时索引都是随机的。 Using iPhone 6s simulator made it go away使用 iPhone 6s 模拟器让它消失了

I changed the isUserInteractionEnabled property of the subviews of Cell to false .我将 Cell 子视图的isUserInteractionEnabled属性更改为false and it works.它有效。

确保单元格是UICollectionViewCell的子类,而不是UICollectionViewReusableView

For me the problem was in Keyboard extension on My BaseViewController对我来说,问题出在 My BaseViewController上的键盘扩展中

extension NewBaseVC: UITextFieldDelegate {

    func initializeHideKeyboard(){
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(
            target: self,
            action: #selector(dismissMyKeyboard))

        view.addGestureRecognizer(tap)
    }

    @objc func dismissMyKeyboard(){
        view.endEditing(true)
    }
}

When I called the initializeKeyboard() , the TapRecognizer was fired.当我调用initializeKeyboard()时,触发了TapRecognizer And the tap recognizer was overlapping with my collection view didSelectItemEvent.并且点击识别器与我的集合视图 didSelectItemEvent 重叠。

Try setting delegate inside尝试在里面设置委托

func awakeFromNib(){
 super.awakeFromNib()
 self.collectionView.dataSource = self
 self.collectionView.delegate = self } 

In my case there was a problem with custom UITapGestureRecognizer .就我而言,自定义UITapGestureRecognizer存在问题。 Despite the fact that the scroll and highlight worked for the collection, there was no cell selection.尽管滚动和突出显示适用于集合,但没有单元格选择。 I had to use cancelsTouchesInView property of the recognizer.我不得不使用识别器的cancelsTouchesInView属性。

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(myAction(_:)))
tapGestureRecognizer.cancelsTouchesInView = false // <- this helped

Also worth to check your collection view cell frame after screen loaded and views layout finished.还值得在屏幕加载和视图布局完成后检查您的集合视图单元格框架。

In my case, it's because collectionView keep crazy reloading.就我而言,这是因为 collectionView 不断疯狂地重新加载。

collectionView.reloadData

Only after end reload then I can interact with it.只有在结束重新加载之后我才能与之交互。

Also check parent view for UITapGestureRecognizer .还要检查UITapGestureRecognizer的父视图。

In my case UITapGestureRecognizer was on the on of the parent view for UICollectionView .在我的例子中, UITapGestureRecognizer位于UICollectionView的父视图之上。

UICollectionView was scrolling ok, but didSelectItemAt was never called. UICollectionView滚动正常,但从未调用过 didSelectItemAt。

In storyboard CollectionView Selectable is uncheck for me.在情节提要中,CollectionView Selectable对我来说是取消选中的。 once you checkmark Selectable then Collectionview didSelectItemsAt will call.一旦选中Selectable ,Collectionview didSelectItemsAt 就会调用。

  1. Go to your storyboard转到您的故事板
  2. Select your CollectionView选择您的 CollectionView
  3. checkmark Selectable复选标记Selectable

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM