How to get indexPath when image inside cell tapped

前端 未结 5 1919
日久生厌
日久生厌 2020-12-06 23:29

I would like to implement feature that if profileImage tapped in tableviewCell, segue to detailView. I add tapGesture but I still can\'t figure out how to get indexPath to p

5条回答
  •  离开以前
    2020-12-07 00:03

    Solution 1 : use touchBegun method, make sure imageView UserInteraction is enable

    override func touchesBegan(_ touches: Set, with event: UIEvent?) {
        let touch = touches.first
          if let cell = touch?.view?.superview?.superview as? YourCellClass {
            let indexPath = yourTablView.indexPath(for: cell)
        //Do whatever you want
        }
    }
    

    Solution 2: you can use button.

    @IBAction func onBtnClick(_ sender : UIButton) {
        if let cell = sender.superview?.superview as? UITableViewCell {
            let indexPath = tbl_songsInfo.indexPath(for: cell)
        //Do whatever you want
        }
    }
    

    Solution 3 : Same as above but use sender.tag

    @IBAction func onBtnClick(_ sender : UIButton) {
        let data = yourDataArray[sender.tag] 
        //Do whatever you want
    }
    

提交回复
热议问题