TableViewController Detects Row Tap with Static Cells

I am using TableViewController, which has a table with two sections of static cells. This is built into the view controller. I cannot get didSelectRowAtIndexPath to work when I click on cells. I have already checked all the usual suspects from this question, as well as this one . When I try with a table view inside a viewcontroller with a dynamic table, I can make it work fine. Is there a problem with using a TableViewController with static cells that would not allow didSelectRowAtIndexPath to be used?

Here is what I have in the custom class for the TableViewController:

import UIKit class OptionTableViewController: UITableViewController { @IBOutlet var optionsTable: UITableView! let numberOfRows = [7,2] let cellIdentifier = "OptionCells" override func viewDidLoad() { super.viewDidLoad() self.optionsTable.delegate = self self.optionsTable.dataSource = self } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func numberOfSections(in tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 2 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows var rows = 0 if(section < numberOfRows.count){ rows = numberOfRows[section] } return rows } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){ print("You selected cell #\(indexPath.row)!") } } 

Update: I tried replacing the tableviewcontroller and the viewcontroller in which it was embedded, but I still cannot run didSelectRowAtIndexPath.

Update 2: Does anyone know if this is possible in Swift 3? I found a working example using Swift 2.2 using tableviewcontroller and static cells here . Maybe there is a bug with Swift 3?

+6
source share
2 answers

Wow, so it turns out that didSelectRowAtIndexPath is no longer valid in Swift 3. Proper use is now done by SelectRowAt. I have not seen this anywhere except this question, which I stumbled upon.

It:

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("You selected cell #\(indexPath.row)!") } 

Not this one :

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){ print("You selected cell #\(indexPath.row)!") } 
+5
source

Perhaps you have the wrong table view connected. Usually, the UITableViewController has a tableView in the view property, and you do not need to configure the data source and delegate it programmatically.

+1
source

All Articles