Hello everyone, I created button in my custom cell, when the user click the that button, I have to segue another class and also I have to pass some data but problem is I could not segue in custom cell, so how can I detect when user tap that button ? or how can I solve this problem
I have to segue another class
What do you mean exactly ? You want to segue from VC1 (which contains table and cells) to another VC ? Is it "I have to segue TO another class"
If I understand correctly your question, you should:
- create a segue from the VC1 which contains TableView to the destination segue. Create from VC1, NOT from the cell.
- Give it an identifier such as "VC1toVC2" (of course, find a more explicit name)
- create a var selectedCell : Int? in VC1
- in VC1, use willSelectRow to know which cell is to be selected (this will be called when you tap the button in the cell)
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
selectedCell = indexPath.row
return indexPath
}
- create the IBAction for the cell button
@IBAction func goNext(_ sender: UIButton) {
self.performSegue(withIdentifier: "VC1toVC2", sender: self)
}
Then in prepare, use selectedCell to figure content as needed and pass the appropriate data.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "VC1toVC2" {
if let destinationController = segue.destination as? VC2ViewController {
destinationController.someContent = dataInVC1[selectedCell] // I assume the data are in such an array, but that depends on the definition of VC1
}
}