I have a custom view which hold a collection view set like below.
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)
}
and delegate functions
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")
}
}
The weird thing is the didHighlightItem function could get called, but didSelectItem will not. Did I missed something here? Thanks for any help.
My views connection is UIViewController(THController) holds the UIView(THEmojisView), THEmojisView holds the collection view. In the THController i've got a lot of views and actions, but not cover the THEmojisView. Is it possible that touchesBegan(_ touches: Set, with event: UIEvent?) of THController would affect the delegate funcs of the collection view ?
Maybe you have a subview
in your Cell, whose isUserInteractionEnabled
is true
.
Try change the isUserInteractionEnabled
property of the subviews of Cell to false
.
This worked for me.
i used a custom layout for my collection view and suddenly the didselect item stopped firing the event.
try add GestureRecognizer
to your 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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With