collectionView didSelectItemAt indexPath未被调用

时间:2017-12-07 03:38:00

标签: ios swift uicollectionview uicollectionviewcell

尝试在我的集合视图中选择一个单元格时,没有任何反应。我设置了我的委托,但是当用户点击时它不会被调用。

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

单元格是包含CAShapeLayer和UILabel的自定义单元格。我试图弄乱isUserInteractionEnabled方法无济于事。

此外,如果我点击速度非常快,我有时可以打印"cell selected"消息,但很少。如果您有任何建议,请告诉我们!

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

1 个答案:

答案 0 :(得分:0)

通过将cancelsTouchesInView方法设置为我false上的UITapGestureRecognizer来解决此问题。完整代码如下。

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