Selector to get indexPath UICollectionView Swift 3.0

后端 未结 5 1526
北恋
北恋 2020-12-21 16:02

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

5条回答
  •  春和景丽
    2020-12-21 16:46

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

提交回复
热议问题