-5

I have a small tableview of 7 rows. I would like to enable different actions for a press of each row.

For example, the row at index 0 would display another VC (without passing data). The row at index 0 would open a web URL, the next might be a share function, etc.

My biggest problem is the first case of send the user to another VC. Mostly because I don't know if I should enable a segue. And for all cases, I need to be able to specify a function for each row separately, therefore I would need to specify the proper index (0,1,2,etc).

How can I do this is Swift?

pretz
  • 232
  • 2
  • 14

1 Answers1

1

Here is a skeleton of code you need:

class ViewController: UITableViewController {

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 2 }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = String(describing: indexPath)
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            print("Do action for row 0")
        } else if indexPath.row == 1 {
            print("Do action for row 1")
        }
    }
}
Frankenstein
  • 15,732
  • 4
  • 22
  • 47