如何使用 Swift 中的 UILongPressGestureRecognizer 通过标签 属性 传递 indexPath.row?

How to pass indexPath.row through the tag property with UILongPressGestureRecognizer in Swift?

这是我的代码:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! imageUserCell

    let coolGesture = UILongPressGestureRecognizer(target: self, action: #selector(detailedPerson.makeItCoolAction(_:)))

    coolGesture.minimumPressDuration = 0.5
    coolGesture.view?.tag = 4

    cell.addGestureRecognizer(coolGesture)

这是函数代码:

   func makeItCoolAction(sender: UIGestureRecognizer){

    print(sender.view?.tag)
    print("Photo cooled")

}

控制台打印相同的值:0,这是默认值。我强烈需要将 indexPath.row 值传递给 makeItCoolAction 但我看到 UIGestureRecognizer 没有标签 属性 但 .view 属性 有它,正如您在代码中看到的那样。

我已将 makeItCoolAction 发送器从 UILongPressGestureRecognizer 更改为 UIGestureRecogizer,它仍然继续打印 0 值。

此时视图为零:

coolGesture.view?.tag = 4

添加手势后尝试设置点击,它的视图如下:

cell.addGestureRecognizer(coolGesture)
coolGesture.view?.tag = 4