'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)")
}


Solution 1:[1]

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)

Solution 2:[2]

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

Solution 3:[3]

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

self.contentView.isUserInteractionEnabled = true

Solution 4:[4]

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

Solution 5:[5]

Select ViewController.swift in Project Navigator. Update the class declaration with this:

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource 


//After class declaration create a new variable:

 var collectionView: UICollectionView!



  override func viewDidLoad() {
   super.viewDidLoad()
     // Do any additional setup after loading the view, typically from a nib.
   let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
   layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
   layout.itemSize = CGSize(width: 90, height: 120)

   collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
   collectionView.dataSource = self
   collectionView.delegate = self
   collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
   collectionView.backgroundColor = UIColor.whiteColor()
   self.view.addSubview(collectionView)
}



func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print("Selected Cell: \(indexPath.row)")
}

Solution 6:[6]

I discovered a strange edge case where I had accidentally specified a class for the cell’s container view (and a class that was not valid for the container view), and everything looked fine, I did not receive any warnings, but didSelectItemAt would not be called.

That's 5 hours of my life I would like back.

Solution 7:[7]

In my case, I was implementing hitTest in UICollectionView's parent view to intercept hit event, so didSelectItemAt did not call.

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
    let p = convert(point, to: v)
    if v.point(inside: p, with: event) {
        return v.hitTest(p, with: event)
    }
    return super.hitTest(point, with: event)
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Milad Faridnia
Solution 2 Ito Katsuyoshi
Solution 3 Dimitar Stefanovski
Solution 4 Massimo Pavanel
Solution 5 Dravidian
Solution 6 Rob
Solution 7 pqteru