如何通过代码实现TableView行点击Segue跳转?代码报错求指导
Hey there, let's walk through the common issues that might be causing your segue to fail, and fix them step by step:
1. Fix the sender Parameter in performSegue
Your current code uses sender: Cell — this is referencing the class type of your cell, not the actual selected cell instance. That's likely causing an issue. Instead, pass the selected cell (or even the indexPath if you prefer) as the sender:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // Pass the selected cell instance let selectedCell = tableView.cellForRow(at: indexPath) performSegue(withIdentifier: "ShowDetailView", sender: selectedCell) // Or alternatively, pass the indexPath directly (useful for data passing later) // performSegue(withIdentifier: "ShowDetailView", sender: indexPath) }
2. Verify Your Segue Setup in Storyboard
Double-check these details in your Storyboard:
- Ensure the segue is connected from your source ViewController (the one with the TableView) to the destination ViewController, not directly from a TableViewCell (unless you want automatic segue triggering without code).
- Confirm the segue's identifier is exactly
ShowDetailView— identifiers are case-sensitive, so make sure there are no typos, extra spaces, or capitalization mismatches.
3. Confirm TableView Delegate/DataSource is Connected
If the didSelectRowAt method isn't even being called (a super common issue!), your TableView isn't talking to your ViewController. Fix this either:
- In Storyboard: Control-drag from the TableView to your ViewController, and select both
delegateanddataSourcefrom the pop-up menu. - Or in code (add this in
viewDidLoad()):
override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self }
Also, make sure your ViewController class declares conformance to the required protocols:
class YourViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { // ... your code ... }
4. Test if didSelectRowAt is Triggered
Add a print statement to confirm the method is actually running when you tap a row:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("Tapped row \(indexPath.row)") // Check your console for this output performSegue(withIdentifier: "ShowDetailView", sender: tableView.cellForRow(at: indexPath)) }
If you don't see this print, go back to step 3 to fix the delegate connection.
5. Add prepare(for:sender:) (If You Need to Pass Data)
If you want to send data to the destination ViewController, implement this method in your source ViewController:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { guard segue.identifier == "ShowDetailView" else { return } // Cast the destination VC to your specific DetailViewController type guard let detailVC = segue.destination as? DetailViewController else { print("Couldn't cast to DetailViewController") return } // Get the selected row's data (adjust this to match your data source) if let indexPath = tableView.indexPathForSelectedRow { let selectedItem = yourDataSourceArray[indexPath.row] detailVC.yourDataProperty = selectedItem } }
Try these steps one by one, and your segue should start working as expected!
内容的提问来源于stack exchange,提问作者Winz

