collectionView didSelectItemAt indexPath不称为swift

Vin*_*Vin 5 ios uicollectionview swift swift3

我有一个自定义视图,其中包含如下所示的集合视图集。

func setupCollectionView() {
    let layout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: scaled(height: 15), left: scaled(width: 35), bottom: scaled(height: 15), right: scaled(width: 35))
    layout.itemSize = CGSize(width: scaled(width: 30), height: scaled(width: 30))
    layout.minimumLineSpacing = 15
    layout.minimumInteritemSpacing = 30
    collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
    collectionView.showsVerticalScrollIndicator = false
    collectionView.showsHorizontalScrollIndicator = false
    collectionView.register(THTexasHoldemEmojiCell.self, forCellWithReuseIdentifier: THTexasHoldemEmojiCell.className)
}
Run Code Online (Sandbox Code Playgroud)

和委托功能

extension THTexasHoldemEmojisView {

    func setupDelegates() {
        collectionView.dataSource = self
        collectionView.delegate = self
    }

}

extension THTexasHoldemEmojisView: UICollectionViewDelegate {

    func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
        print("did highlight item")
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("did select item")

    }

}
Run Code Online (Sandbox Code Playgroud)

奇怪的是didHighlightItem函数可以被调用,但didSelectItem不会。我在这里错过了什么吗?谢谢你的帮助。

我的视图连接是UIViewController(THController)持有UIView(THEmojisView),THEmojisView持有集合视图。在THController中,我有很多视图和操作,但没有涵盖THEmojisView。THController的touchesBegan(_ touches:Set,with event:UIEvent?)是否有可能影响集合视图的委托函数?

Arv*_*aei 5

我为我的集合视图使用了自定义布局,突然 didselect 项目停止触发事件。

尝试添加GestureRecognizer到您的UICollectionView

let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))

self.collectionView.addGestureRecognizer(tap)

self.collectionView.isUserInteractionEnabled = true


@objc func handleTap(_ sender: UITapGestureRecognizer) {
   if let indexPath = self.collectionView?.indexPathForItem(at: sender.location(in: self.collectionView)) {
//Do your stuff here

}
}
Run Code Online (Sandbox Code Playgroud)


Li *_*Jin 5

也许您的 Cell 中有一个subview,其名称isUserInteractionEnabledtrue.

尝试将isUserInteractionEnabledCell 的子视图的属性更改为false

这对我有用。


Ais*_*abu 0

THTexasHoldemEmojiCell可能存在问题。也许集合视图单元格内有一个UIControl实例来处理您的所有触摸。简单的解决方案是将此UIControl的isUserInteractionEnabled属性设置为 false。