How to navigate different screens in Swift using the didSelectRowAtIndexPath method?

0

I need to click on one of the array positions to go to a specific screen. DidSelectRowAtIndexPath method shows black screen and not the screen it should. Here's the code I've done so far:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!



var frutas = ["Macã", "Laranja"]

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.dataSource = self
    self.tableView.delegate = self
    self.title = "Agua"





    //self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}


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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

    let gesture = UILongPressGestureRecognizer(target: self, action: "showAlerta:")

    cell.addGestureRecognizer(gesture)

    let linha  = frutas[indexPath.row]
    cell.textLabel?.text = "\(linha)"

    return cell
}


 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {



    var viewController: UIViewController

    if indexPath.row == 0 {
     viewController = DetalheViewController()
     navigationController?.pushViewController(viewController , animated: true)

    }


    if indexPath.row == 1 {
     viewController = DetalhesDois()
     navigationController?.pushViewController(viewController , animated: true)

    }


}

func showAlerta(sender: UILongPressGestureRecognizer){

    if sender.state == UIGestureRecognizerState.Began {
        var alerta = UIAlertController(title: "", message: "", preferredStyle: UIAlertControllerStyle.Alert)

        let show = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler:nil)

        alerta.addAction(show)

        presentViewController(alerta,animated:true,completion:nil)
    }
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "detalhe" {

        let destino = segue.destinationViewController as! DetalheViewController
    }
    if segue.identifier == "detalhedois" {

        let destino = segue.destinationViewController as! DetalhesDois
    }

}

override func performSegueWithIdentifier(identifier: String?, sender: AnyObject?) {
    <#code#>
}




}
    
asked by anonymous 17.07.2015 / 19:42

1 answer

1

With the addition of self.performSegueWithIdentifier("detalhe", sender: self) within didSelectRowAtIndexPath , I was able to solve the problem.

Follow updated code:

import UIKit

class ViewController: UIViewController , UITableViewDataSource, UITableViewDelegate{
    @IBOutlet weak var tableView: UITableView!

    var frutas = ["Macã", "Laranja"]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.title = "Agua"

        //self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

        let gesture = UILongPressGestureRecognizer(target: self, action: "showAlerta:")

        cell.addGestureRecognizer(gesture)

        let linha  = frutas[indexPath.row]
        cell.textLabel?.text = "\(linha)"

        return cell
    }

     func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


        if indexPath.row == 0 {
            self.performSegueWithIdentifier( "detalhe", sender: self)
        }

        if indexPath.row == 1 {

            self.performSegueWithIdentifier( "detalhedois", sender: self)
        }
    }

    func showAlerta(sender: UILongPressGestureRecognizer){
        if sender.state == UIGestureRecognizerState.Began {
            var alerta = UIAlertController(title: "", message: "", preferredStyle: UIAlertControllerStyle.Alert)

            let show = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler:nil)

            alerta.addAction(show)

            presentViewController(alerta,animated:true,completion:nil)
        }
    }
}
    
18.07.2015 / 17:35