选择器获取indexPath UICollectionView Swift 3.0 [英] Selector to get indexPath UICollectionView Swift 3.0

查看:112
本文介绍了选择器获取indexPath UICollectionView Swift 3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击两次时,我试图在单元格上获得 indexPath
我在 Selector 中传递参数,但是它给出了错误。
这是什么格式?

I'm trying to get indexPath on the cell when it is tapped twice. I'm passing arguments in Selector like this but it is giving error. What is the correct format for this ?

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if  let subOptioncell : SubOptionsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: subOptionsCVReuseIdentifier, for: indexPath) as! SubOptionsCollectionViewCell

          let imageNamed = "\(customizeOptionSelected[indexPath.row])"
          subOptioncell.subOptionsImage.image = UIImage(named: imageNamed)

          let tap =  UITapGestureRecognizer(target: self, action: #selector(doubleTapped(sender: indexPath)))
          tap.numberOfTapsRequired = 2
          collectionView.addGestureRecognizer(tap)   

          return subOptioncell
    }
}

func doubleTapped(sender: IndexPath) {
    print("Double Tap")
}


推荐答案

首先,您要将tapGesture添加到 collectionView 而不是 subOptioncell

First of all you are adding tapGesture to collectionView instead of subOptioncell.

subOptioncell.addGestureRecognizer(tap)



而不是:



Instead of:

collectionView.addGestureRecognizer(tap)

你好nnot使用选择器 UIGestureRecognizer 传递其他实例,您可以传递的唯一实例是 UI(抽头)GestureRecognizer 。如果你想要那个单元格的indexPath,你可以尝试这样。首先设置你的选择器 TapGesture 就像这样。

You cannot pass other instance with selector of UIGestureRecognizer, the only instance you can pass is UI(Tap)GestureRecognizer. If you want the indexPath of that cell you can try like this. First of all set your selector of TapGesture like this.

let tap =  UITapGestureRecognizer(target: self, action: #selector(doubleTapped(sender:)))

现在方法应该是:

func doubleTapped(sender: UITapGestureRecognizer) {
    if let cell = sender.view as? SubOptionsCollectionViewCell, let indexPath = self.collectionView.indexPath(for: cell) {
         print(indexPath)
    }       
}

编辑:如果要在单元格双击上显示/隐藏图像,则需要使用 indexPath处理它 code>的单元格,首先声明一个 IndexPath 的实例,并在 cellForItemAt indexPath 中使用它。 / p>

If you want to show/hide image on cell double tap then you need to handle it using indexPath of cell, for that first declare one instance of IndexPath and use it inside cellForItemAt indexPath.

var selectedIndexPaths = IndexPath()

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    //Your code 
    //Now add below code to handle show/hide image
    cell.subOptionSelected.isHidden = self.selectedIndexPaths != indexPath
    return cell
}

现在 doubleTapped 的行动 UITapGestureRecognizer 设置 selectedIndexPath

func doubleTapped(sender: UITapGestureRecognizer) {
    if let cell = sender.view as? SubOptionsCollectionViewCell, let indexPath = self.collectionView.indexPath(for: cell) {
         if self.selectedIndexPaths == indexPath {
             cell.subOptionSelected.isHidden = true
             self.selectedIndexPaths = IndexPath()
         }
         else {
             cell.subOptionSelected.isHidden = false
             self.selectedIndexPaths = indexPath
         }           
    }       
}

这篇关于选择器获取indexPath UICollectionView Swift 3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆