Trigger an action when the user clicks on a cell quickly

I am creating an application using Swift. I have a UITableView that I populate with some data from a database. When a user clicks on a cell, I would like to trigger an action.

What I've done:

 var array: [String] = ["example"] func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return array.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = UITableViewCell(style :UITableViewCellStyle.Default, reuseIdentifier: "cell") cell.textLabel.text = array[indexPath.row] cell.tag = indexPath.row cell.targetForAction("getAction:", withSender: self) return cell } func getAction(sender:UITableViewCell)->Void { if(sender.tag == 0) { println("it worked") } } 

I tried to adapt the solution from another post, but I definitely did it wrong. Thank you in advance

+8
ios uitableview swift
source share
2 answers

didSelectRowAtIndexPath UITableViewDelegate and use didSelectRowAtIndexPath

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 

you can use indexPath to get an item from your collection and perform your action

+17
source share

In Swift 4, I believe the method has changed a bit:

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // your code } 
+5
source share

All Articles