如何根据同一视图控制器中选定的 tableView 单元更新标签?

How do I update labels based on a selected tableView cell that is in the same View Controller?

我在 ViewController 中有一个 tableView,它还有一个名为 infoView 的单独视图。我没有第二个 viewController。 infoView 和 tableView 在同一个 ViewController 中,我需要 infoView 中的标签在点击特定单元格时更新。我创建了一个单独的函数来更新标签,但是每当我调用该函数时它什么都不做。这是我的 viewController 的屏幕截图,以便更好地理解。 ViewController

这是我的代码:

 @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var symbol: UILabel!
    @IBOutlet weak var elementName: UILabel!
    
    @IBOutlet weak var atomicNum: UILabel!
    @IBOutlet weak var atomicWeight: UILabel!
    @IBOutlet weak var meltingPoint: UILabel!
    @IBOutlet weak var boilingPoint: UILabel!
    @IBOutlet weak var yearDiscovered: UILabel!
    @IBOutlet weak var textView: UITextView!
    
    
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(selectedCell))
    
    
    
    //Info Source - Wikipedia because I don't really care if it's correct.
    //Temps are in Fahrenheit
    //I realize this isn't a complete list of radioactive elements. Don't Science Me!
    let elementArray:[(symbol: String, name: String, atomicNum: Int, atomicWeight:Double, meltPoint:Double?, boilPoint:Double?, notes:String, discYear:Int )] = [
        ("Pu", "Plutonium", 94, 244, 1182.9, 5842, "Prepared by bombardment of uranium with deuterons.", 1941),
        ("U", "Uranium", 92, 238.02891, 2070, 4404, "Mistakenly identified a uranium oxide obtained from pitchblende as the element itself and named it after the recently discovered planet Uranus.", 1789),
        ("Pm", "Promethium", 61, 145, 1908, 5432, "It was probably first prepared in 1942 by bombarding neodymium and praseodymium with neutrons, but separation of the element could not be carried out. Isolation was performed under the Manhattan Project in 1945.", 1942),
        ("Rn", "Radon", 86, 222, nil, nil, "E. Dorn discovered a radioactive gas resulting from the radioactive decay of radium, isolated later by W. Ramsay and R.W. Gray.", 1898),
        ("Th", "Thorium", 90, 232.0377, 3182, 8650, "J. Berzelius obtained the oxide of a new earth in thorite.", 1829),
        ("Ra", "Radium", 88, 226, 1760, 3159, "The Curies reported on December 26, 1898, a new element different from polonium, which Marie later isolated from uraninite.", 1898),
        ("Ds", "Darmstadtium", 110, 281, nil, nil, "Prepared by bombardment of lead with nickel.", 1994),
        ("Po", "Polonium", 84, 209, 489, 1764, "In an experiment done on July 13, 1898, the Curies noted an increased radioactivity in the uranium obtained from pitchblende, which they ascribed to an unknown element.", 1898),
        ("Fr", "Francium", 87, 223, 80 , 1250, "Perey discovered it as a decay product of Ac. Francium is the last element to be discovered in nature, rather than synthesized in the lab, although some of the synthetic elements that were discovered later (plutonium, neptunium, astatine) were eventually found in trace amounts in nature as well.", 1939),
        ("Ac", "Actinum", 89, 227, 2240, 5800, "A.L. Debierne obtained from pitchblende a substance that had properties similar to those of thorium.", 1899),
        ("Cm", "Curium", 96, 247, 2444, 5630, "Prepared by bombarding plutonium with alpha particles during the Manhattan Project", 1944),
        ("Es", "Einsteinium", 99, 252, 1580, 1825, "Formed in the first thermonuclear explosion in November 1952, by irradiation of uranium with neutrons; kept secret for several years.", 1952)
    ]
    
    func numberOfSections(in tableView: UITableView) -> Int { return 1; }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return elementArray.count}
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "elementCell", for: indexPath) as! ElementTableViewCell
        
        cell.setupCell(elementArray[indexPath.row].symbol, name: elementArray[indexPath.row].name)
        cell.addGestureRecognizer(tapGesture)
        
        
        
        return cell
    }
    
    @objc func selectedCell() {
        
        //display selected cell info in info View
        if let indexpath = tableView.indexPathForSelectedRow {
            let element = elementArray[indexpath.row]
            symbol.text = element.symbol
            elementName.text = element.name
            atomicNum.text = element.atomicNum.description
            atomicWeight.text = element.atomicWeight.description
            yearDiscovered.text = element.discYear.description
            textView.text = element.notes
            if let boiling = element.boilPoint {
                boilingPoint.text = boiling.description
            } else {
                boilingPoint.text = "N/A"
            }
            if let melt = element.meltPoint {
                meltingPoint.text = melt.description
            } else {
                meltingPoint.text = "N/A"
            }
            
        }
        
        
    }

    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
    }

我希望这个问题是有道理的。提前致谢!

很好的第一次尝试,强烈建议您探索 UITableViewDelegate 在那里你可以看到你的 ViewController 是否是 table 视图的委托你可以做类似于

的事情
extension MyViewController: UITableViewDelegate {
    func tableView(UITableView, didSelectRowAt: IndexPath) {
        updateLabels(elementArray[indexPath.row])
    }
}

因为你似乎是 iOS 开发的新手 我还要提一下你应该让这个视图控制器成为你的 table 视图的委托,这意味着 table 视图将调用函数当某些事件发生时,你的 viewController。 您可以如何执行此操作的示例是:

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.delegate = self
    }

但是,如果您的视图控制器是 UITableViewController 的子class,只需将函数添加到您的 class。

注意: 添加 didSelectRowAt 方法,这是 UITableview 的委托方法之一,当您点击单元格时会调用该方法。

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let items = elementArray[indexPath.row]
             updateInfo(item: items)
    }
    
    func updateInfo(item : (symbol: String, name: String, atomicNum: Int, atomicWeight:Double, meltPoint:Double?, boilPoint:Double?, notes:String, discYear:Int ))
        {
            symbol.text = item.symbol
            elementName.text = item.name
            atomicNum.text = item.atomicNum.description
        }