如何获取 tableView 的 indexPath 并在 metod prepare 上使用 indexPath(for segue:)

How to get the indexPath of tableView and use the indexPath on the metod prepare(for segue: )

我有一个显示 7 行部分的 Tableview,我想做的是当我按下第 0 行时将它发送到另一个视图,table 中的所有信息都由代码加载使用修复程序,我的问题是如何在 "prepare ()" 方法中获取 indexPath 以及如何以编程方式完成 segue。

这是我的 class 我的 TableView 中有所有代码:

//
//  ControlTablaPrincipalTableViewController.swift
//  Seccion 15
//
//  Created by Barbatos on 7/10/18.
//  Copyright © 2018 Seccion 15. All rights reserved.
//

import UIKit

class ControlTablaPrincipalTableViewController:UITableViewController {

    var titulos : [String] = ["Caja de ahorro","Blog de la Seccion 15","Iniciar Sesion","Galeria de eventos","Convenios", "Ubicacion", "Contactanos"]

    var imagenes : [UIImage] = [#imageLiteral(resourceName: "caja"),#imageLiteral(resourceName: "blog"),#imageLiteral(resourceName: "sesion"),#imageLiteral(resourceName: "galeria"),#imageLiteral(resourceName: "convenio"),#imageLiteral(resourceName: "ubicacion"),#imageLiteral(resourceName: "contacto")]

    override func viewDidLoad() {

        super.viewDidLoad()

        self.tableView.dataSource = self
        self.tableView.delegate = self

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return titulos.count

    }

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

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
        cell?.imageView!.image = imagenes[indexPath.row]
        cell?.textLabel?.text = titulos[indexPath.row]

        return cell!
    }

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

        let alert = UIAlertController(title: "Celda seleccionada", message: "Se selecciono la celda \(indexPath.row)",preferredStyle: .alert)

        let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)

        alert.addAction(okAction)
        self.present(alert, animated: true, completion: nil)
    }

这是我的 didSelectRowAt 方法,我在其中编写了一个警报,它会显示我正在编写哪个单元格:

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

        let alert = UIAlertController(title: "Celda seleccionada", message: "Se selecciono la celda \(indexPath.row)",preferredStyle: .alert)

        let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)

        alert.addAction(okAction)        
        self.present(alert, animated: true, completion: nil)   
   }

这是 prepare 方法,我尝试恢复选定的单元格并编写 if 以便能够使用我的 segue:

overrider func prepare(for segue: UIStoryboardSegue, sender: Any?){

    if let indexPath = tableView.indexPathForSelectedRow{
        let selectedRow = indexPath.row
            if(selectedRow == 0){
               segue.destination as? CajadeAhorroViewController 
            }
    }

}

我的 segue 的标识符是 "caja":

我想你只想在 tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 函数中执行 segue...

override func tableView(_ tableView: UITableView, 
           didSelectRowAt indexPath: IndexPath) {
    switch indexPath.row {
    case 0:
        performSegue(withIdentifier: "caja", sender: nil)
    case 1:
        performSegue(withIdentifier: "whatever you set this identifier to", sender: nil)
    // cases for all the different rows...
    }
}

如果你只有一个 segue 并且你需要切换视图控制器的变量,它会根据所选的单元格显示不同

overrider func prepare(for segue: UIStoryboardSegue, sender: Any?){
    if let indexPath = tableView.indexPathForSelectedRow,
       let destination = segue.destination as? CajadeAhorroViewController {
        switch indexPath.row {
        case 0:
            // destination is the "CajadeAhorroViewController", set its properties for how you want it.
        case 1: 
        // ... all 7 cases ...
        }
    }
}