UIViewController中UITableViewCell点击跳转ChatController失效问题求助
Hey there, let's work through this segue issue together—since you've already got the UITableView delegate/datasource set up and added a segue with an identifier, here are the most common fixes to get that navigation working:
1. Verify Your Segue Setup
- First, make sure your segue is connected from the MessagesController itself (the yellow view controller icon in Storyboard) to the ChatController, not directly from a UITableViewCell. If you connect from the cell, the system triggers the segue automatically, but since you're using
didSelectRowAt, you need a controller-to-controller segue. - Double-check that the segue's identifier in Storyboard matches exactly what you use in code (it's case-sensitive!). For example, if you call
performSegue(withIdentifier: "NavigateToChat", sender: self), the Storyboard segue's identifier must be exactly "NavigateToChat".
2. Ensure didSelectRowAt is Actually Triggered
Add a debug print to confirm the method is being called when you tap a cell:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("Cell at row \(indexPath.row) was selected!") // Your existing code here }
If you don't see this print in the console:
- Check that the UITableView's
delegateanddataSourceoutlets are connected to the MessagesController in Storyboard (drag from the tableView to the yellow view controller icon and select delegate/dataSource). - Alternatively, manually set them in
viewDidLoadas a fallback:override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self }
3. Correctly Call performSegue in didSelectRowAt
Make sure you're explicitly triggering the segue inside the selection method. Here's a complete example:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { myIndex = indexPath.row // Store the selected index if you need to pass data performSegue(withIdentifier: "YourSegueIdentifier", sender: self) tableView.deselectRow(at: indexPath, animated: true) // Optional: Deselect the cell after tap }
If you need to pass data to ChatController, use the prepare(for:sender:) method:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { guard segue.identifier == "YourSegueIdentifier", let chatVC = segue.destination as? ChatController else { return } // Pass your data, e.g.: chatVC.selectedMessageIndex = myIndex }
4. Check for a Navigation Controller
If you're using a "push" segue, your MessagesController must be embedded in a UINavigationController. If it's not, the push won't work—either embed it in a nav controller (Editor > Embed In > Navigation Controller) or switch the segue type to "modal" (though push is more intuitive for a chat flow).
5. Rule Out Cell Interaction Issues
Make sure your UITableViewCell hasn't had user interaction disabled. Check that the cell's isUserInteractionEnabled property is set to true (it's enabled by default, but custom cells might override this accidentally).
内容的提问来源于stack exchange,提问作者Lily S.

