select器来获取indexPath UICollectionView Swift 3.0

我试图获得两次点击单元格上的indexPath 。 我在这样的Selector传递参数,但它给错误。 这是什么正确的格式?

 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

它应该是:

 subOptioncell.addGestureRecognizer(tap) 

代替:

 collectionView.addGestureRecognizer(tap) 

您不能通过UIGestureRecognizer selector传递其他实例,您可以传递的唯一实例是UI(Tap)GestureRecognizer 。 如果你想要那个单元格的indexPath,你可以尝试像这样。 首先,像这样设置TapGestureselector

 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处理它,首先声明IndexPath一个实例,并在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 } 

现在在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 } } } 

在你的情况下正确的select器doubleTapped: 那是

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

调用目标方法时不能激发任意参数。 你可以在subOptioncell上设定目标

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

你可以在subOptioncell设置任意的subOptioncell

你需要像这样添加select器

 let tap = UITapGestureRecognizer(target: self, action: #selector(YourViewControllerName.doubleTapped(_:))) subOptioncell.addGestureRecognizer(tap) 

改变你的代码。

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

和function。

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

在你的数据源方法

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { if let subOptioncell : SubOptionsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: subOptionsCVReuseIdentifier, for: indexPath) as! SubOptionsCollectionViewCell{ //... your code subOptioncell.addGestureRecognizer(tap) return subOptioncell } } 

然后在函数cellTapped()

 func cellTapped(sender: UITapGestureRecognizer){ let tapLocation = sender.location(in: yourCollectionView) let indexPath : IndexPath = yourCollectionView.indexPathForItem(at: tapLocation)! var currentIndex = 0 if let cell = yourCollectionView.cellForItem(at: indexPath){ currentIndex = cell.tag } print("Your Selected Index : \(currentIndex)") } 

快乐编码!