gpt4 book ai didi

ios - tableView.dequeueReusableCell 打开错误的ViewController

转载 作者:行者123 更新时间:2023-11-28 11:55:11 25 4
gpt4 key购买 nike

我有一个数组,我为每个数组项创建了一个单独的 vc,tableView.dequeueReusableCell 显示错误的 vc。我读到这是因为该单元格是可重复使用的,并且它保留了先前选择的数组中的数据。你能帮我解决一下吗?

我的代码是:

import UIKit

class Nicosia: UIViewController, UITableViewDelegate, UITableViewDataSource {
let nicosiaPlaces = ["Famagusta Gate", "Laiki Geitonia", "Ledra Street","Omeriye Hamam","Cyprus Museum","Venetian Walls","The House of Hatjigeorgakis Kornessios","Byzantine Art Museum","Archbishop's Palace","Liberty Monument","The Faneromeni Church","Nicosia International Conference Center"]

var identities = ["Famagusta Gate", "Laiki Geitonia", "Ledra Street","Omeriye Hamam","Cyprus Museum","Venetian Walls","The House of Hatjigeorgakis Kornessios","Byzantine Art Museum","Archbishop's Palace","Liberty Monument","The Faneromeni Church","Nicosia International Conference Center"]

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
let city = nicosiaPlaces [indexPath.row]
cell?.textLabel?.text = city
return cell!
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
}

最佳答案

你的意思是didDeselectRowAt,而不是dequeueReusableCell

问题是您需要 didSelectRowAt,而不是 didDeselectRowAt

关于ios - tableView.dequeueReusableCell 打开错误的ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51223961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com