user11547760
user11547760

Reputation:

How to segue into a new view controller from button in table view cell

How can I segue into a new view controller from a button that is in a table view cell? I need to send some data with it so I'm not sure if I can do that from my tableViewCell class.

Upvotes: 2

Views: 576

Answers (2)

Noah Iarrobino
Noah Iarrobino

Reputation: 1543

try

var profilePressed: ((UITableViewCell) -> Void)?

@IBAction func profileNamePressed(_ sender: Any) {
    profilePressed?(self)
}

in your table view cell class, and this:

cell.profilePressed = { (cell) in
    let profileVC = self.storyboard?.instantiateViewController(withIdentifier: "ProfileVC") as! ProfileVC
    profileVC.initData(withPostedBy: message.postedBy)
    self.presentDetail(profileVC)
}

in your cell for row at index path function

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100503

Structure the custom cell like

class MyCell:UITableViewCell { 

  weak var delegate:VCName? 

  @IBAction func btnClicked(_ sender:UIButton) {
     delegate?.performSegue(withIdentifier:"segue",sender:someValue)
  }

}

And inside the vc's cellForRowAt

let cell = ///
cell.delegate = self

Upvotes: 0

Related Questions