how to give function in didselectrowat in swift

To give a function in didSelectRowAt method of a UITableView in Swift, follow the steps below:

  1. Override didSelectRowAt method of the UITableViewDelegate in your ViewController:
main.swift
class ViewController: UIViewController, UITableViewDelegate {
    
    // ...
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // call your function here
        yourFunctionName()
    }
    
    // ...
    
}
258 chars
13 lines
  1. Define your function wherever you want in the same ViewController:
main.swift
class ViewController: UIViewController, UITableViewDelegate {
    
    // ...
    
    func yourFunctionName() {
        // do whatever you want here
    }
    
    // ...
    
}
179 chars
12 lines

You can replace yourFunctionName with any name of your choice. Remember to define the function outside the didSelectRowAt method.

gistlibby LogSnag