Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why UICollectionView didSelect method does not work?

I've created my UICollectionView programmatically and in this case my didSelectItemAtIndexPath method does not call at all.

let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: CGRectGetWidth(self.view.frame), height: 360), collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.userInteractionEnabled = true

So, what is a problem? Why when I tap on the cells I do not get my response?

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print("Selected Cell: \(indexPath.row)")
}
like image 672
Doe Avatar asked Aug 19 '16 10:08

Doe


4 Answers

I just encountered this case.

Did you put a button or an interactive view in a collection view cell? Your touch is handled by it, then didSelect isn't called.

Turn off User Interaction Enabled property by Interface builder, or set false programmatically.

enter image description here

like image 191
Ito Katsuyoshi Avatar answered Sep 19 '22 15:09

Ito Katsuyoshi


Be sure that you don't have UICollectionViewDelegate and UICollectionViewDelegateFlowLayout together.

like image 30
Massimo Pavanel Avatar answered Sep 19 '22 15:09

Massimo Pavanel


i Know this may be late, but for the sakes of feature purpose, CollectionView[didSelectItem] not responding to touch could be as a result of you also enalbing addGestureRecognizer so check if you have set a gesture recognizer on your CollectionView like so collectionView?.addGestureRecognizer(tapGestRecognizer)

like image 31
Ekene Mefor Avatar answered Sep 20 '22 15:09

Ekene Mefor


check in UiCollectionviewCell subclass isUserInteractionEnabled property or set it programaticaly to true

self.contentView.isUserInteractionEnabled = true
like image 45
Dimitar Stefanovski Avatar answered Sep 21 '22 15:09

Dimitar Stefanovski