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

Asked

Viewed 681 times

0

I need you to click on one of the positions of array, the same goes for a specific screen. didSelectRowAtIndexPath method shows black screen and not the screen it should. Follow the code I made until then:

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#>
}




}
  • 1

    This answer here is exactly what you need. See if it is according to what you need.

  • Partially solved because when I click on one of the cells, a black screen appears. println() works

  • You can update your question with your new attempt so we can better identify the error?

  • I was able to solve it another way, how do I share this code with the solution for others to see?

  • 1

    You can answer your own question and after 2 days you can mark it as the correct ;)

1 answer

1


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

Follows 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)
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.