Performing a segue from a button within a custom UITableViewCell

I found an swiftier answer for passing data with a button in a dynamic cell to a new VC.

  1. Setting up a Tableview, a custom cell (connect the cell with your created class and set a cell-identifier) and create a new VC and connect it via a segue (also set an identifier for the segue) to your current TableViewController

  2. setting up your cell with the outlet (cellButton)

    class CustomCell: UITableViewCell {
        @IBOutlet weak var cellButton: UIButton!
    
    
       override func awakeFromNib() {
           super.awakeFromNib()
       }
    
       override func setSelected(_ selected: Bool, animated: Bool) {
          super.setSelected(selected, animated: animated)
       }
    }
    
  3. CellForRowAt function in the TableViewController-Class

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomCell
    
        //...Do stuff for your cell
    
        cell.cellButton.tag = indexPath.row //or value whatever you want (must be Int)
        cell.cellButton.addTarget(self, action: #selector(TableViewController.buttonTapped(_:)), for: UIControlEvents.touchUpInside)
     }
    
  4. set up the button method that's called by the target of the button

    func buttonTapped(_ sender:UIButton!){
        self.performSegue(withIdentifier: "customSegueIdentifier", sender: sender)
    }
    
  5. prepare for segue function

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if(segue.identifier == "customSegueIdentifier") {
            if let destination = segue.destination as? YourNewViewController {
    
               if let button:UIButton = sender as! UIButton? {
                   print(button.tag) //optional
                   destination.valueViaSegue = button.tag
               }
            }
        }
    }
    

Hope this code will help you guys. keep coding ;)


Create a Protocol with the Method, that will be called by the CustomCell's Delegate, defined on your TableViewController

//Pass any objects, params you need to use on the 
//segue call to send to the next controller.

protocol MyCustomCellDelegator {
    func callSegueFromCell(myData dataobject: AnyObject)
}

Now use the Protocol on your UITableViewController

class MyTableViewController : UITableViewController, MyCustomCellDelegator {


 //The usual Defined methods by UIViewController and UITableViewController 

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

  //Set the CustomCell new Delegate
  var cell = tableView.dequeueReusableCellWithIdentifier(customIdentifier) as MyCustomCell


  cell.delagete = self

  return cell

 }


 //MARK: - MyCustomCellDelegator Methods

 func callSegueFromCell(myData dataobject: AnyObject) {
   //try not to send self, just to avoid retain cycles(depends on how you handle the code on the next controller)
   self.performSegueWithIdentifier("showComments", sender:dataobject )

 }

}

Define the Delegate on your Custom Cell and the Call inside your New Button the Delegate Function.

class MyCustomCell : UITableViewCell {

      var delegate:MyCustomCellDelegator!
      @IBOutlet weak var myButton:UIButton



     @IBAction func buttonPressed(sender:AnyObject){
           var mydata = "Anydata you want to send to the next controller"
           if(self.delegate != nil){ //Just to be safe.
             self.delegate.callSegueFromCell(mydata)
           }
    }
}

Hopefully this can be clean and clear for you to understand and implement in your code.