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?

Community
  • 1
  • 1
user1715916
  • 333
  • 5
  • 13

2 Answers2

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)!")
}
Community
  • 1
  • 1
user1715916
  • 333
  • 5
  • 13
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.

Randall
  • 14,691
  • 7
  • 40
  • 60
  • 1
    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. – user1715916 Oct 11 '16 at 17:27