Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect tableView cell touched or clicked in swift

I'm trying to get index of selected item in TableView and start some activity after that. Unfortunately most of solutions that I found are in objective-c or do not work.

Method func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) don't print the cell label..

Can somebody help me please?

import UIKit import ResearchKit  class TaskListViewController: UIViewController, UITableViewDataSource {          let tasks=[("Short walk"),         ("Audiometry"),         ("Finger tapping"),         ("Reaction time"),         ("Spatial span memory")     ]               //how many sections are in your table     func numberOfSectionsInTableView(tableView: UITableView) -> Int {         return 1     }          //return int how many rows     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {         return tasks.count     }          //what are the contents          func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> 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) -> String? {          return "Tasks"              }          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()              }   } 

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...

import UIKit import ResearchKit  class TaskListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {          @IBOutlet     var tableView: UITableView?     var items: [String] = ["We", "Heart", "Swift"]          override func viewDidLoad() {         super.viewDidLoad()                  self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")     }               func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {         return self.items.count;     }          func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {         var cell:UITableViewCell = self.tableView!.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell                  cell.textLabel?.text = self.items[indexPath.row]                  return cell     }          func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {         println("You selected cell #\(items[indexPath.row])!")     }      }  
like image 868
pawisoon Avatar asked Jul 02 '15 11:07

pawisoon


People also ask

How do you get the indexPath row when a button in a cell is tapped?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.

What is indexPath in Tableview Swift?

Swift version: 5.6. Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section.

What is UITableView in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+

How do I populate UITableView?

There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.


2 Answers

If you want the value from cell then you don't have to recreate cell in the didSelectRowAtIndexPath

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {     println(tasks[indexPath.row]) } 

Task would be as follows :

let tasks=["Short walk",     "Audiometry",     "Finger tapping",     "Reaction time",     "Spatial span memory" ] 

also you have to check the cellForRowAtIndexPath you have to set identifier.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {     let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell     var (testName) = tasks[indexPath.row]     cell.textLabel?.text=testName     return cell } 

Hope it helps.

like image 78
Ashish Kakkad Avatar answered Oct 29 '22 13:10

Ashish Kakkad


In Swift 3.0

You can find the event for the touch/click of the cell of tableview through it delegate method. As well, can find the section and row value of the cell like this.

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {        print("section: \(indexPath.section)")        print("row: \(indexPath.row)") } 
like image 23
jaiswal Rajan Avatar answered Oct 29 '22 12:10

jaiswal Rajan