7

I'm using a TableViewController that has a table with 2 sections of static cells. This is embedded in a view controller. I cannot get didSelectRowAtIndexPath to run when I tap the cells. I've already check all of 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 am able to get it to work just fine. Is there an issue with using a TableViewController with static cells that would not allow for using didSelectRowAtIndexPath?

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 it was embedded in but I am still not able to get didSelectRowAtIndexPath to run.

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

4
  • There are some useful things to try here that you may find useful. stackoverflow.com/a/20020180/1655378 Commented Oct 11, 2016 at 17:12
  • Thanks but I had actually already tried those suggestions as well. Commented Oct 11, 2016 at 17:21
  • How do you embed the TVC in the view controller? Commented Oct 11, 2016 at 19:27
  • @pbasdf Using an embed segue from a container view inside the view controller. Commented Oct 11, 2016 at 19:28

2 Answers 2

6

Wow, so it turns out that didSelectRowAtIndexPath is no longer correct in Swift 3. The correct usage is now didSelectRowAt. I didn't see this mentioned anywhere except this question which I stumbled upon.

This:

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

Not This:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){ print("You selected cell #\(indexPath.row)!") } 
Sign up to request clarification or add additional context in comments.

Comments

1

It's possible you have the wrong table view hooked up. Normally, a UITableViewController has it's tableView in the view property and you don't need to set up the data source and delegate programatically.

1 Comment

I double check and even "re-hooked" it up. I figured that I didn't need the data source and delegate but put them in anyway when I couldn't get it to work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.