Selector to get indexPath UICollectionView Swift 3.0

落爺英雄遲暮 提交于 2020-01-20 08:39:55

问题


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

回答1:


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



回答2:


The correct selector in your case is doubleTapped:. That is

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

You can not fire arbitrary parameter when the target method is called. You can set target on subOptioncell by

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

And you can set whatever arbitrary object.parameter you want in subOptioncell




回答3:


You need to add Selector like this way

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



回答4:


Change your code to.

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

and the function to.

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



回答5:


At your Datasource method

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
       }

}

Then at the function 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)")
}

Happy Coding!!!



来源:https://stackoverflow.com/questions/41259366/selector-to-get-indexpath-uicollectionview-swift-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!