Open a new view by pressing an item of a table in Swift 3

1

I have a problem with a table created in Xcode8 and Swift 3, my table is generated by code I mean that all the contents of the table are loaded from arrays in its class and with the methods "numberOfSections" and "numberOfRowsInSection", I generate my table when the application is executed, but now my question is: how can I program the segue so that depending on the selected cell I open a new view, so I have my Main. storyboard:

I have my Table View Controller and the ones I have there are so that when I press cell 0, I send it to Savings and if I press cell 1, I send it to another view.

The complete code of the class where this table is is this:

//
//  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)

        if(indexPath.row == 0){

        }

    }
    
asked by Enrique Espinosa 11.07.2018 в 16:33
source

1 answer

1

1- To go from one viewController to another, in didSelectRowAtIndexPath you must do the Segue perform:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.indexPath = indexPath
    performSegue(withIdentifier: "segue", sender: nil)
}

2- If you want to pass some data from the source viewcontroller to the second viewcontroller you must overwrite the prepare for Segue:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "segue"{
        let vc = segue.destination as! SecondViewController
        guard let index = indexPath else{return}
        vc.indexPath = index
    }
}

In this example I am passing the indexPath of the table that you select to the second ViewController.

Personally I would recommend you to instantiate the Storyboard and do a present or a push if you have a navigation controller:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "Second") as! SecondViewController
        controller.indexPath = indexPath
        self.present(controller, animated: true, completion: nil)
//        Si tienes un navigation controller usa esta line
//        self.navigationController?.pushViewController(controller, animated: true)
    }
    
answered by 11.07.2018 / 19:09
source