在tabelview中选择行时看到了各自的视图控制器

时间:2017-10-25 11:39:27

标签: ios swift uitableview

我想在tableView中选择相应的行时显示不同的ViewController。但我无法修复它,因为它在为该控制器选择相应的行后显示随机视图控制器我检查了ViewControllers的所有标识符,但它仍然以错误的方式显示 这是我写的代码:

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    switch(indexPath.row) {
        case 0 : 
            let v1 = self.storyboard?.instantiateViewController(withIdentifier: "pass1") as! ViewControllerngodetails
            navigationController?.pushViewController(v1, animated: true)
            break

        case 1 : 
            let v2 = self.storyboard?.instantiateViewController(withIdentifier: "pass2") as! ViewController2Reg
            navigationController?.pushViewController(v2, animated: true)
            break

        case 2 : 
            let v3 = self.storyboard?.instantiateViewController(withIdentifier: "pass3") as! ViewController3FCRA
            navigationController?.pushViewController(v3, animated: true)        
            break

        case 3 : 
            let v4 = self.storyboard?.instantiateViewController(withIdentifier: "pass4") as! ViewController4SecIssue
            navigationController?.pushViewController(v4, animated: true)
            break

        case 4 : 
            let v5 = self.storyboard?.instantiateViewController(withIdentifier: "pass5") as! ViewController5contactdet
            navigationController?.pushViewController(v5, animated: true)
            break

        case 5 : 
            let v6 = self.storyboard?.instantiateViewController(withIdentifier: "pass6") as! ViewController6bnkdetails
            navigationController?.pushViewController(v6, animated: true)
            break

       default: 
            break
    }
}

3 个答案:

答案 0 :(得分:0)

您展示的代码看起来很好。问题可能在于输入参数(indexPath)。 func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)

(我建议您使用断点来识别导致问题的代码)

答案 1 :(得分:0)

您永远不会取消选择该行。也许您已经为表视图启用了多个选择并保留了之前的选择?尝试在切换之前将其放在didSelectRowAt方法中:

tableView.deselectRow(at: indexPath, animated: true)

编辑:我刚看到您使用委托方法didDeselectRowAt,您应该使用didSelectRowAt。那可能是你的问题。更改后,didSelectRowAt方法应该在执行任何其他操作之前取消选择该行。

答案 2 :(得分:0)

仅供参考您可以将所有VC标识符存储在数组中,也可以使用didSelect方法调用。

let arrIdentifiers = ["pass1","pass2","pass3","pass4","pass5","pass6"]

didSelect of tableView:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vc = self.storyboard?.instantiateViewController(withIdentifier: arrIdentifiers[indexPath.row])
    self.navigationController?.pushViewController(vc!, animated: true)
    tableView.deselectRow(at: indexPath, animated: true)
}