简体   繁体   中英

collectionView didSelectItemAt indexPath not being called

When attempting to select a cell in my collection view, nothing happens. I have my delegate set up but it does not get called when the user taps.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("cell selected")
}

The cell is a custom cell containing a CAShapeLayer and a UILabel. I have attempted to mess around with the isUserInteractionEnabled method to no avail.

In addition, I can sometimes get the "cell selected" message to print if I tap really fast but it is very seldom. Let me know if you have any suggestions!

func drawCircle() {
    var circleLayer: CAShapeLayer!

    let arcCenter = CGPoint(x: frame.size.width / 2.0, y: (frame.size.height / 2.0) - 15)
    let circlePath = UIBezierPath(arcCenter: arcCenter, radius: (frame.size.width - 20) / 2, startAngle: 0.0, endAngle: CGFloat(.pi * 2.0), clockwise: true)

    circleLayer = CAShapeLayer()
    circleLayer.path = circlePath.cgPath
    circleLayer.fillColor = UIColor.clear.cgColor
    circleLayer.strokeColor = UIColor(named: "Fourth")?.cgColor
    circleLayer.lineWidth = 8.0

    circleLayer.strokeEnd = 1.0

    // Add the circleLayer to the view's layer's sublayers
    layer.addSublayer(circleLayer)
}

This was fixed by setting cancelsTouchesInView method to false on a UITapGestureRecognizer I have. Full code below.

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.hideKeyboardByTappingOutside))
    tap.cancelsTouchesInView = false
    self.view.addGestureRecognizer(tap)

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