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
Accepted Reply
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
}
}
-
No, I created custom cell for my tableView, and I connected that custom cell with my tableview which is in my ViewController, so when user click the that button(in my customCell), I have to segue another page, but I could not do segue in custom cell, also I could not reach indexPath of that button
Replies
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
}
}
-
No, I created custom cell for my tableView, and I connected that custom cell with my tableview which is in my ViewController, so when user click the that button(in my customCell), I have to segue another page, but I could not do segue in custom cell, also I could not reach indexPath of that button
So does it work now (you marked the answer as correct) ?
You can in fact find the indexPath of the cell in which the button is.
A simple way is
- to set a tag for each cell, equal to its row (if there is a single section ; for multi section, you should multiplex the row with 1000*section for instance).
- in the button action, call its superView (you may have to call superview.superview until you reach a cell class or nil).
- check it is a cell and of the correct type.
- find its tag and rebuild the indexPath (or use row directly).
-
No, that marked was a mistake. Also I couldnt understand your second comment :(