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

如何将TableViewCell的值保存至下一个TableViewController的列表中?

实现方案(以iOS Swift为例)

1. 定义统一数据模型

先创建一个结构体来标准化Cell的内容,方便传递和存储:

struct CellItem: Codable {
    let title: String
    let detail: String? // 根据你实际的Cell字段调整
}

2. 源ViewController(提供内容的页面)处理

假设源页面是ContentSourceVC,内部包含TableView,以下是两种常用的内容传递方式:

方式一:闭包传递(轻量场景推荐)

在源VC中定义闭包属性,用来回调选中的内容:

class ContentSourceVC: UIViewController {
    // 存储源页面的Cell数据
    private var sourceData: [CellItem] = [
        CellItem(title: "示例1", detail: "详情1"),
        CellItem(title: "示例2", detail: "详情2")
    ]
    
    // 闭包:传递选中的Cell内容
    var onContentSelected: ((CellItem) -> Void)?

    // TableView选中事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let selectedItem = sourceData[indexPath.row]
        onContentSelected?(selectedItem)
        navigationController?.popViewController(animated: true)
    }
}

方式二:代理模式(复杂场景推荐)

先定义代理协议,再在源VC中设置代理属性:

// 代理协议
protocol ContentSourceDelegate: AnyObject {
    func sourceVC(_ vc: ContentSourceVC, didPickItem item: CellItem)
}

class ContentSourceVC: UIViewController {
    private var sourceData: [CellItem] = [/* 初始化数据 */]
    weak var delegate: ContentSourceDelegate?

    // TableView选中事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let selectedItem = sourceData[indexPath.row]
        delegate?.sourceVC(self, didPickItem: selectedItem)
        navigationController?.popViewController(animated: true)
    }
}

3. 目标TableViewController(展示内容的页面)处理

目标页面TargetListVC需要接收数据并更新TableView:

闭包方式的接收逻辑

在跳转到源VC时绑定闭包,接收数据后刷新列表:

class TargetListVC: UITableViewController {
    // 存储要展示的内容列表
    private var displayList: [CellItem] = []

    // 跳转到源页面的按钮事件
    @IBAction func openSourceVC(_ sender: UIButton) {
        guard let sourceVC = storyboard?.instantiateViewController(withIdentifier: "ContentSourceVC") as? ContentSourceVC else { return }
        
        sourceVC.onContentSelected = { [weak self] selectedItem in
            self?.displayList.append(selectedItem)
            self?.tableView.reloadData()
        }
        
        navigationController?.pushViewController(sourceVC, animated: true)
    }

    // TableView数据源方法
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return displayList.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TargetCell", for: indexPath)
        let item = displayList[indexPath.row]
        cell.textLabel?.text = item.title
        cell.detailTextLabel?.text = item.detail
        return cell
    }
}

代理方式的接收逻辑

让目标VC遵守代理协议,实现代理方法接收数据:

class TargetListVC: UITableViewController, ContentSourceDelegate {
    private var displayList: [CellItem] = []

    @IBAction func openSourceVC(_ sender: UIButton) {
        guard let sourceVC = storyboard?.instantiateViewController(withIdentifier: "ContentSourceVC") as? ContentSourceVC else { return }
        sourceVC.delegate = self
        navigationController?.pushViewController(sourceVC, animated: true)
    }

    // 实现代理方法
    func sourceVC(_ vc: ContentSourceVC, didPickItem item: CellItem) {
        displayList.append(item)
        tableView.reloadData()
    }

    // 同样实现TableView数据源方法...
}

4. 可选:数据持久化

如果需要退出APP后保留列表内容,可使用UserDefaults存储:

// 保存数据
if let encodedData = try? JSONEncoder().encode(displayList) {
    UserDefaults.standard.set(encodedData, forKey: "SavedCellItems")
}

// 页面初始化时读取数据
override func viewDidLoad() {
    super.viewDidLoad()
    if let savedData = UserDefaults.standard.data(forKey: "SavedCellItems") {
        displayList = try? JSONDecoder().decode([CellItem].self, from: savedData)
        tableView.reloadData()
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 04:15:15