fgroeger
fgroeger

Reputation: 422

Detect UITableViewCell Click in custom Controller

I have an UITableViewController with some static cells embedded into an UIViewController using a container view. Within this UITableViewController I need the possibility to toggle the visibility of a cell containing an UIPickerView. I tried the following within my TableViewController to detect a tap on the cell but it didn't print anything:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath: NSIndexPath) {
    print("Tap detected.", didSelectRowAtIndexPath)
}

I also tried working with reuse identifiers but it returned null at every cell.

Can you tell me how to detect a tap on a cell within the TableViewController? Complete code attached.

http://pastebin.com/r12Yv9Be

Upvotes: 2

Views: 199

Answers (3)

bughana
bughana

Reputation: 324

I checked your code and saw that you are adding a tap gesture recognizer to the whole view. It might be that this stops touches from being transferred to the tableview cells. I would try removing that gesture recognizer and then try again if you reach the didSelect function when clicking the cells.

Upvotes: 1

axel
axel

Reputation: 422

If you setted correctly the delegate and datasource of tableView, you should change the function to the following and see if you actually print the row:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
      print("Tap detected. Selected indexPath is: \(indexPath.row)")
}

Upvotes: 1

Umair Afzal
Umair Afzal

Reputation: 5039

Try to update your viewDidLoad method as follows

override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
}

Upvotes: 1

Related Questions