问题
If cell is touched - move on the another VC, how can i do it? I tried to do it, but i didn't work. Here is my code
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "transData" {
let destination = segue.destination as! WinnerViewController
let cell = sender as? UITableViewCell
if cell == cell {
let indexPath = tableView.indexPath(for: cell!)
if indexPath == indexPath {
let productName = users[(indexPath?.row)!]
destination.winner = productName.name
}
}
}
When i touch each cell - it isn't moving. Please help
回答1:
There are two options to perform a segue when a cell is tapped.
The segue is connected from the table view cell to the destination controller
In this case the cell is passed as the
senderparameteroverride func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "transData", let cell = sender as? UITableViewCell { let destination = segue.destination as! WinnerViewController let indexPath = tableView.indexPath(for: cell)! let productName = users[indexPath.row] destination.winner = productName.name } }The segue is connected from the source controller to the destination controller
In this case you have to implement
didSelectRowAt, callperformSegue(withIdentifierand pass the index path assenderparameterfunc tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { performSegue(withIdentifier: "transData", sender: indexPath) }Then
prepare(forhas to beoverride func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "transData", let indexPath = sender as? IndexPath { let destination = segue.destination as! WinnerViewController let productName = users[indexPath.row] destination.winner = productName.name } }
回答2:
Hi you can do like this :
take Indexpath as golbal variable eg : var indexPathTemp : IndexPath?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
indexPathTemp = indexPath
performSegueWithIdentifier("transData", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "transData" {
let destination = segue.destination as! WinnerViewController
let productName = users[(indexPath?.row)!]
destination.winner = productName.name
}
}
回答3:
EDIT
The answer from @vadian is detailed and correct. The following are just other ways of navigating from a view controller to another.
If you use segue, and want to navigate to the WinnerViewController:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
performSegue(withIdentifier: "YOUR_SEGUE_IDENTIFIER", sender: indexPath)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "YOUR_SEGUE_IDENTIFIER", let indexPath = sender as? IndexPath {
let destinationVC = segue.destination as? WinnerViewController
let productName = users[indexPath.row]
destinationVC.winner = productName.name
}
}
If you use Storyboard and pushViewController:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let productName = users[indexPath.row]
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let destinationVC = storyboard.instantiateViewController(withIdentifier: "YOUR_WINNER_VIEW_IDENTIFIER") as? WinnerViewController else { return}
destinationVC.winner = productName.name
navigationController?.pushViewController(destinationVC, animated: true)
}
If you use Interface Builder and pushViewController:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let productName = users[indexPath.row]
let destinationVC = WinnerViewController(nibName: "YOUR_WINNER_VIEW_NAME", bundle: nil)
destinationVC.winner = productName.name
navigationController?.pushViewController(destinationVC, animated: true)
}
来源:https://stackoverflow.com/questions/44996910/if-cell-is-touched-move-on-the-another-vc-swift