How to use didSelectRowAtIndex?

1

I'd like to know how to use didSelectRowAtIndex , more specifically I'd like to know how each row of my UITableView calls a ViewController specified. In my code below, in case you would like every item in the array that is in UITableView call another ViewController .

class ViewControllerAnalise: UIViewController, UITableViewDataSource, UITableViewDelegate {
    var formulas = ["Participaçao de Capital de Terceiros", "Composiçao do Endividamento", "Imobilizaçao do Patrimonio Liquido", "Liquidez Geral", "Liquidez Corrente", "Liquidez Seca", "Giro do Ativo", "Margem Liquida", "Rentabilidade do Ativo", "Rentabilidade do PL"]

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ViewControllerCell2

        cell.formula.text = formulas[indexPath.row]

        return cell
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return formulas.count
    }
}
    
asked by anonymous 17.07.2015 / 13:58

1 answer

2

Eduardo, it's very simple but it also depends on how your structure is. The way you explained it is very superficial, so I'll make some assumptions.

To do this, simply identify which cell is pressed and tell which screen will open for each situation.

It would look like this, for example:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var viewController: UIViewController

    switch indexPath.row {
    case 0: viewController = ParticipacaoCapitalViewController()
    case 1: viewController = ComposicaoViewController()
    case 2: viewController = ImobilizacaoViewController()
    default: viewController = nil
    }

    if viewController != nil
        navigationController?.pushViewController(viewController, animated: true)
}

Here too I'm assuming that you're using UINavigationController , so the pushViewController , otherwise it will be necessary to open it another way.

    
17.07.2015 / 14:19