How to get the index path for a custom table cell when the UIImage inside it is used and captured by UITapGestureRecognizer in prepareForSegue

Well, a lot of variables in the name, sorry, I could not make it simpler.

Firstly, I have a custom table cell with such descriptions

enter image description here

now when the user clicks on the cell itself, it will go to View A, however there is a UITapGestureRecognizer that is connected to the UIImage on the left, which is connected to the segment that goes to View B.

Everything is fine, but I need some data that is inside the table view cell, which I can pass to View B so that it can do some things after the view is displayed.

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if(segue.identifier == "toViewA") {

    }else if( segue.identifier == "toViewBFromThatImage" ){

        let viewBVC = ( segue.destinationViewController as! viewBVC )
        ///////sender is UITapGestureRecognizer 
        ///////somehow want to get indexpath here

         viewBVC.selectedIndex = SOMEHOW DIRREVED INDEXPATH
    }
}

, - UIImage ? ?

, .

0
1

, segue . , , , .

-

protocol MyCustomCellDelegate {
    func imageTappedInCell(cell:MyCustomCell)
}

delegate -

class MyCustomCell {

    weak var delegate : MyCustomCellDelegate?

    ...

   func imageTapped(recognizer:UIGestureRecognizer) {

       if (recognizer.state == .Ended) {
           delegate?.imageTapped(self)
       }

   }

Then in your view controller you can implement the delegate method. In the delegate method, you can use a cell to identify the index path

 class MyTableViewController: UIViewController,MyCustomCellDelegate {


     func imageTapped(cell:MyCustomCell) {
         self.performSegueWithIdentifier("toViewBFromThatImage",sender:cell)
     }

     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if( segue.identifier == "toViewBFromThatImage" ){
            let viewBVC = ( segue.destinationViewController as! viewBVC )
            let senderCell=sender as! MyCustomCell
            viewBVC.selectedIndex = self.tableview.indexPathForCell(senderCell)!
        }
    }
}
+3
source

Source: https://habr.com/ru/post/1611735/


All Articles