A
A
Aleksandr Govorukhin2016-11-15 21:06:34
iOS
Aleksandr Govorukhin, 2016-11-15 21:06:34

How to implement clicking on a cell and switching to another swift window?

Hello, friends!
How to implement so that by clicking on one of the cells in the TableView, I could go to a specific ViewController. For example, by clicking on 1 cell, open Push Name1ViewController, on 2 cell Name2ViewController and so on. Didn't find info anywhere. Little experience, please. Thanks in advance for your time.919f5b00f0184a028a60f534350f6221.png2d828ccc6e9a4be39af6d5a13084d413.png60673d769f95460180679c3c55ee6dd9.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
ManWithBear, 2016-11-15
@SnapSh0t

We wrap these controllers in a separate object (cloudure as an option) and process it by tapping on the corresponding cell.
UPD. Example:

struct Row {
    let title: String
    let generator: (Parameters) -> UIViewController
}

let section: [Row] = [
    Row(title: "first", generator: { (parameters) -> UIViewController in
        return FirstViewController()
    }),
    Row(title: "second", generator: { (parameters) -> UIViewController in
        return SecondViewController()
    })
]

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let row = section[indexPath.row]
    let vc = row.generator(Parameters)
    navigationController?.pushViewController(vc, animated: true)
}

I
Igor Cherny, 2016-11-16
@freeg0r

You also have the didSelectRowAt indexPath method: which is called when you click on the cell, it contains both the cell number and the section number, and put the necessary controllers (not themselves, but information about which controller is needed for this cell) into an array with indices corresponding to cell numbers. Just like you get the text for the cell in CellForRowAt indexPath: from the names array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question