简体   繁体   中英

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

我知道这可能会迟到,但出于功能目的, CollectionView[didSelectItem]没有响应触摸可能是因为您还启用了addGestureRecognizer因此请检查您是否在CollectionView上设置了手势识别器,例如collectionView?.addGestureRecognizer(tapGestRecognizer)

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.

在此处输入图片说明

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

检查UiCollectionviewCell子类isUserInteractionEnabled属性或将其编程设置为 true

self.contentView.isUserInteractionEnabled = true

确保您没有将 UICollectionViewDelegate 和 UICollectionViewDelegateFlowLayout 放在一起。

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM