You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

模态视图内UITableView无法触发didSelectRowAt事件排查求助

问题解决:模态ViewController中UITableView单元格点击无响应

核心问题定位

你代码里的didSelectRowAt方法写在了ViewController的扩展里,但实际负责表格逻辑的是RecipeCategoryViewController,而表格的delegate绑定的是这个RecipeCategoryViewController,所以这个点击方法根本不会被触发。

修复步骤

  1. 将didSelectRowAt方法移到RecipeCategoryViewController内部,或者它的专属扩展中:
class RecipeCategoryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    // ... 原有代码 ...
    
    // 添加点击方法到类内部
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("clicked")
        performSegue(withIdentifier: "showRecipeDetail", sender: nil)
    }
}

或者用扩展形式:

extension RecipeCategoryViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("clicked")
        performSegue(withIdentifier: "showRecipeDetail", sender: nil)
    }
}

注:原ViewController扩展中的该方法可直接删除,与当前业务逻辑无关

额外检查点

  • 确认自定义单元格RecipeCategoryTableViewCell内的所有子控件(如UILabel)的userInteractionEnabled属性为false,若子控件开启用户交互,会拦截单元格的点击事件。
  • 可在代码中显式设置表格选择属性,与Storyboard配置保持一致:
override func viewDidLoad() {
    super.viewDidLoad()
    tableView.rowHeight = 300.0
    tableView.allowsSelection = true // 显式开启单元格选择
    fetchMeals()
}

内容的提问来源于stack exchange,提问作者Papi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 08:06:21