title: How to detect tableView cell touched or clicked in swift
body: <p>I'm trying to get <code>index</code> of selected item in <code>TableView</code> and start some activity after that. Unfortunately most of solutions that I found are in objective-c or do not work.</p>
  <p>Method <code>func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)</code> don't print the <code>cell</code> label..</p>
  <p>Can somebody help me please?</p>
  <pre class="lang-swift prettyprint-override"><code>import UIKit
  import ResearchKit
  
  class TaskListViewController: UIViewController, UITableViewDataSource {
      
      let tasks=[(&quot;Short walk&quot;),
          (&quot;Audiometry&quot;),
          (&quot;Finger tapping&quot;),
          (&quot;Reaction time&quot;),
          (&quot;Spatial span memory&quot;)
      ]
      
      
      //how many sections are in your table
      func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
          return 1
      }
      
      //return int how many rows
      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return tasks.count
      }
      
      //what are the contents
      
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
          var cell = UITableViewCell()
          
          var (testName) = tasks[indexPath.row]
          cell.textLabel?.text=testName
          return cell
      }
      
      // give each table section a name
      
      func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -&gt; String? {
  
          return &quot;Tasks&quot;
          
      }
      
      func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
          
          let indexPath = tableView.indexPathForSelectedRow();
          
          let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!
          
          println(currentCell.textLabel!.text)
      }
      
      
      override func viewDidLoad() {
          super.viewDidLoad()
          
      }  
  }
  </code></pre>
  <p>After a few tries I changed the code to a different one from tutorial that I found. And it doesn't work too. Now I'm thinking this is the issue with iOS simulator...</p>
  <pre class="lang-swift prettyprint-override"><code>import UIKit
  import ResearchKit
  
  class TaskListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
      
      @IBOutlet
      var tableView: UITableView?
      var items: [String] = [&quot;We&quot;, &quot;Heart&quot;, &quot;Swift&quot;]
      
      override func viewDidLoad() {
          super.viewDidLoad()
          
          self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: &quot;cell&quot;)
      }
      
      
      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return self.items.count;
      }
      
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
          var cell:UITableViewCell = self.tableView!.dequeueReusableCellWithIdentifier(&quot;cell&quot;) as! UITableViewCell
          
          cell.textLabel?.text = self.items[indexPath.row]
          
          return cell
      }
      
      func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
          println(&quot;You selected cell #\(items[indexPath.row])!&quot;)
      }
      
  }
  
  </code></pre>