How to use didSelectRowAtIndex?

Asked

Viewed 58 times

1

I would like to know how to use didSelectRowAtIndex, more specifically I would like to know how each Row of my UITableView calls a ViewController specifies. In my code below, in case would like each item of the array who is in the 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
    }
}

1 answer

2


Eduardo, it’s very simple but it also depends on how your structure is. The way you explained it is pretty shallow, so I’m gonna make some assumptions.

To do this, all you have to do is identify which cell is pressed and tell which screen will be opened 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 guessing you’re using UINavigationController, therefore the pushViewController, otherwise it will be necessary to open otherwise.

  • In this case Voce passed the value of the cell to the second screen right? , in the code I posted has an array with 10 elements, I wanted when I clicked on each of the elements to open a different viewcontroller, in this case I have 10 viewcontrollers,and each element calls one. I don’t understand the detail. I’m sorry but I’m still getting started

  • Okay, I made the changes to my answer to suit your case.

  • Thank you very much, it worked out here, :)

  • Perfect, if this answer has been helpful, consider mark it as settled, so help others with the same doubt and community :)

  • Really, can you leave the other code here tbm? to send the indexPath.Row to another viewController

  • Yes, thence inside each ViewController you will need a variable of type Int, and then just define, for example, viewController.itemIndex = indexPath.row before giving the push.

  • Thank you very much, where I put the question as resolved?

  • To mark an answer as accepted, click the checkmark next to the answer to switch from gray to green color.

Show 3 more comments

Browser other questions tagged

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