You need to enable JavaScript to run this app. iOS开发:UITableView选中行索引传递至ViewController值错乱问题求助
优惠活动
大模型
产品
解决方案
定价
更多

iOS开发:UITableView选中行索引传递至ViewController值错乱问题求助

问题排查与修复方案

核心问题定位

  • 未实现跨控制器传值的核心方法prepare(for:sender:),跳转时没有将选中行的索引或对应球洞名传递给ScoreViewController实例
  • 将holes、myIndex定义为全局变量,这种写法会在控制器重复实例化、多次跳转时出现值被意外覆盖的问题,是索引错乱的直接原因
  • 存在segue重复触发风险:如果Storyboard中GoToScore的segue是直接从UITableViewCell拖拽连线的,点击cell时系统会自动触发一次segue,你在didSelectRowAt中又手动调用performSegue,会导致跳转两次,索引被第二次触发的逻辑覆盖
  • ScoreViewController中用于展示球洞名的text属性从未被赋值,无法正常显示选中的球洞信息

修复步骤

  1. 移除全局变量,将holes改为OverViewController的实例属性,myIndex改为OverViewController的私有属性存储选中行索引
  2. 统一segue触发逻辑:二选一即可,避免重复跳转:
    • 方案A:删除Storyboard中cell到ScoreViewController的segue,保留didSelectRowAt中的手动performSegue调用
    • 方案B:删除didSelectRowAt中的performSegue调用,保留cell直接连线的segue,系统会自动触发跳转
  3. 在OverViewController中实现prepare(for:sender:)方法,完成数据传递
  4. 调整ScoreViewController的赋值逻辑,接收到传值后直接赋值给标题标签

修复后核心代码示例

OverViewController 修复后代码

import UIKit

class OverViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    // 移除全局变量,改为实例属性
    private let holes = ["Hole 1","Hole 2","Hole 3","Hole 4","Hole 5","Hole 6","Hole 7","Hole 8", "Hole 9","Hole 10","Hole 11","Hole 12","Hole 13","Hole 14","Hole 15","Hole 16","Hole 17","Hole 18"]
    // 存储选中行索引,改为私有属性
    private var selectedRow: Int = 0

    @IBOutlet weak var BackButton: UIButton!
    @IBOutlet weak var ScoreButton: UIButton!
    @IBOutlet weak var OverviewLabel: UILabel!
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func BackToStartScreen(_ sender: UIButton) {
        performSegue(withIdentifier: "BackToStart", sender: self)
    }

    @IBAction func GoToResultScreen(_ sender: UIButton) {
        performSegue(withIdentifier: "GoToResult", sender: self)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return holes.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = holes[indexPath.row]
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedRow = indexPath.row
        // 如果是方案A(从VC手动连segue),保留下面这行;如果是方案B(cell连segue),删除下面这行
        performSegue(withIdentifier: "GoToScore", sender: self)
    }

    // 实现传值方法
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "GoToScore" {
            guard let scoreVC = segue.destination as? ScoreViewController else { return }
            // 直接传递选中的球洞名,也可以改为传递索引按需取值
            scoreVC.holeName = holes[selectedRow]
        }
    }
}

ScoreViewController 修复后核心代码

class ScoreViewController: UIViewController {
    // 新增接收球洞名的属性,如需传索引可额外新增`var holeIndex: Int = 0`
    var holeName: String = ""

    @IBOutlet weak var TitleLabel: UILabel!
    // 其余原有属性、方法保持不变...

    override func viewDidLoad() {
        super.viewDidLoad()
        // 原有初始化逻辑保持不变...
        // 直接使用传过来的holeName赋值
        TitleLabel.text = holeName
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.05 17:15:04