Vin
Vin

Reputation: 81

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 ?

Upvotes: 8

Views: 13548

Answers (7)

bola gamal
bola gamal

Reputation: 109

I had the same problem, you should make sure you didn't using tapGestureRecognizer on your view in the background of collectionView.

Upvotes: 0

Li Jin
Li Jin

Reputation: 2147

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.

Upvotes: 5

Sasho
Sasho

Reputation: 3652

Should you have a Gesturerecognizer for a parent view or viewcontroller's view - set it's cancelsTouchesInView = false

Upvotes: 2

Arvin Rezaei
Arvin Rezaei

Reputation: 888

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

}
}

Upvotes: 5

Firda Sahidi
Firda Sahidi

Reputation: 1281

just call

collectionView.dataSource = self
collectionView.delegate = self

in your viewDidLoad

Upvotes: 1

desmond_yy
desmond_yy

Reputation: 83

I got the similar problem.I have my custom UIView and add collectionView on it.I also set the datasource and delegate but I can't scroll the collectionView and the didSelectItemAt function didn't called. The point is that I set the wrong frame of collectionView

Upvotes: 1

Aist Marabu
Aist Marabu

Reputation: 169

There could be a problem with THTexasHoldemEmojiCell. Maybe there is a UIControl instance inside collection view cell that handles all your touches. Simple solution is setting isUserInteractionEnabled property of this UIControl to false.

Upvotes: 0

Related Questions