tableView.dequeueReusableCell打开错误的ViewController

时间:2018-07-07 14:06:33

标签: ios swift uitableview

我有一个数组,并且为每个数组项创建了单独的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
    }
}

2 个答案:

答案 0 :(得分:1)

您的意思是didDeselectRowAt,而不是dequeueReusableCell

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

答案 1 :(得分:1)

替换此

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

使用

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)