Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
970 views
in Technique[技术] by (71.8m points)

ios - Selector to get indexPath UICollectionView Swift 3.0

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")
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

It should be:

subOptioncell.addGestureRecognizer(tap)

Instead of:

collectionView.addGestureRecognizer(tap)

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

Edit: 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
}

Now on doubleTapped action of UITapGestureRecognizer set the 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
         }           
    }       
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...