![](/img/trans.png)
[英]collectionView didSelectItemAt indexPath not being called
[英]collectionView didSelectItemAt indexPath not called swift
我有一个自定义视图,其中包含一个集合视图集,如下所示。
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)
}
和委托功能
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")
}
}
奇怪的是 didHighlightItem function 可以被调用,但 didSelectItem 不会。 我在这里错过了什么吗? 谢谢你的帮助。
我的视图连接是 UIViewController(THController) 持有 UIView(THEmojisView),THEmojisView 持有集合视图。 在 THController 中,我有很多视图和操作,但不包括 THEmojisView。 THController 的 touchesBegan(_ touches: Set, with event: UIEvent?) 是否可能会影响集合视图的委托功能?
也许您的 Cell 中有一个subview
,其isUserInteractionEnabled
为true
。
尝试将 Cell 子视图的isUserInteractionEnabled
属性更改为false
。
这对我有用。
我为我的集合视图使用了自定义布局,突然 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
}
}
我遇到了类似的问题。我有我的自定义 UIView 并在其上添加了 collectionView。我还设置了数据源和委托,但我无法滚动 collectionView 并且没有调用 didSelectItemAt 函数。 重点是我把collectionView的frame设置错了
打电话
collectionView.dataSource = self
collectionView.delegate = self
在你的 viewDidLoad
THTexasHoldemEmojiCell可能存在问题。 也许在集合视图单元格中有一个UIControl实例来处理您的所有触摸。 简单的解决方案是将此UIControl 的isUserInteractionEnabled属性设置为 false。
如果您有父视图或视图cancelsTouchesInView = false
视图的cancelsTouchesInView = false
- 将其设置为cancelsTouchesInView = false
我有同样的问题,你应该确保你没有在collectionView
的背景下对你的view
使用tapGestureRecognizer
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.