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

如何从嵌套在TableViewCell中的TableViewCell跳转至ViewController?

解决嵌套TableView中Cell点击无法跳转的问题

兄弟,我太懂这种嵌套TableView跳转卡壳的痛苦了!折腾一天肯定头大,我帮你捋捋问题出在哪,怎么解决👇

核心问题其实很简单:你的tableView2是放在UITableViewCell(也就是SegmentedCell)里面的,而Cell本身不是ViewController,没有navigationController属性,直接在Cell里调用push肯定没用。得把点击事件传递给上层的ViewController(也就是tableView1所在的那个VC),让它来执行跳转操作。

下面是具体的解决步骤:

步骤1:给SegmentedCell添加点击回调闭包

先在你的SegmentedCell类里定义一个闭包,用来把嵌套Cell的点击事件传递出去:

import UIKit
import XMSegmentedControl
import Alamofire
import SwiftyJSON

class SegmentedCell: UITableViewCell, XMSegmentedControlDelegate, UITableViewDelegate, UITableViewDataSource {
    // 定义闭包,参数可以根据需求传indexPath或者数据模型
    var didSelectNestedCell: ((IndexPath) -> Void)?
    
    // 假设你的tableView2是这个属性
    @IBOutlet weak var tableView2: UITableView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // 确保tableView2的代理和数据源设置正确
        tableView2.delegate = self
        tableView2.dataSource = self
        // 确保用户交互开启(默认是true,但保险起见加上)
        tableView2.isUserInteractionEnabled = true
    }
    
    // 实现tableView的点击方法
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // 触发闭包,把点击的位置传递给上层VC
        didSelectNestedCell?(indexPath)
        // 可以顺便取消Cell选中状态
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    // ... 你已有的其他代理方法(numberOfRows、cellForRow等)
}

步骤2:在上层ViewController中接收回调并执行跳转

在tableView1所在的ViewController里,给SegmentedCell设置闭包,在闭包里完成push操作:

class YourMainVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
    // 假设你的tableView1是这个属性
    @IBOutlet weak var tableView1: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView1.delegate = self
        tableView1.dataSource = self
        // 注册SegmentedCell
        tableView1.register(UINib(nibName: "SegmentedCell", bundle: nil), forCellReuseIdentifier: "SegmentedCell")
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SegmentedCell", for: indexPath) as! SegmentedCell
        
        // 这里可以设置cell的其他数据,比如给tableView2传数据源
        
        // 设置嵌套Cell的点击回调
        cell.didSelectNestedCell = { [weak self] nestedIndexPath in
            guard let self = self else { return }
            // 1. 创建你要跳转的目标ViewController
            let targetVC = YourTargetViewController()
            // 2. 传递数据(如果需要,比如根据nestedIndexPath取对应的数据)
            // targetVC.itemData = yourDataSourceArray[nestedIndexPath.row]
            // 3. 执行跳转(这里的navigationController是上层VC的,肯定有效)
            self.navigationController?.pushViewController(targetVC, animated: true)
        }
        
        return cell
    }
    
    // ... 你已有的其他tableView1代理方法
}

额外排查点

如果还是不行,先检查这几个地方:

  • 确认tableView2的delegate和dataSource确实绑定到了SegmentedCell,没有被其他代码覆盖
  • 在SegmentedCell的didSelectRowAt里加个print("嵌套Cell被点击了!"),看看方法有没有被触发
  • 确保你的上层ViewController确实被放在UINavigationController里,不然navigationController会是nil,push操作自然没反应

这样改完应该就能顺利跳转了,嵌套UI的核心就是把事件向上传递,让有权限的组件来执行操作~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:13:16