Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

collectionView didSelectItemAt indexPath not called swift

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 ?

like image 337
Vin Avatar asked Dec 29 '16 11:12

Vin


2 Answers

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.

like image 174
Li Jin Avatar answered Sep 28 '22 04:09

Li Jin


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

}
}
like image 37
Arvin Rezaei Avatar answered Sep 28 '22 06:09

Arvin Rezaei