选择器获取 indexPath UICollectionView Swift 3.0 [英] Selector to get 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)
代替:
collectionView.addGestureRecognizer(tap)
你不能通过UIGestureRecognizer
的selector
传递其他实例,你可以传递的唯一实例是UI(Tap)GestureRecognizer
.如果您想要该单元格的 indexPath,您可以像这样尝试.首先像这样设置TapGesture
的selector
.
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:)))
现在方法应该是这样的:
Now method should be like:
func doubleTapped(sender: UITapGestureRecognizer) {
if let cell = sender.view as? SubOptionsCollectionViewCell, let indexPath = self.collectionView.indexPath(for: cell) {
print(indexPath)
}
}
如果你想在单元格双击显示/隐藏图像,那么你需要使用单元格的 indexPath
处理它,首先声明一个 IndexPath
并在 cellForItemAt indexPath
中使用它.
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
}
现在在UITapGestureRecognizer
的doubleTapped
动作上设置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屋!