UITableViewController's didSelectRowAtIndexPath doesn't work

uitableviewcontroller delegate
uitableviewcontroller programmatically swift
uitableviewcontroller vs uitableview
uitableviewcontroller to uiviewcontroller
uitableviewcontroller example objective-c
uitableviewcontroller datasource
storyboard uitableviewcontroller
uitableviewcontroller objective-c

I am using an UITableViewController for it and I override the following method and try to enable different segues when the user select different row.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print("You selected row #\(indexPath.row)!")
    switch indexPath.row {
    case 3:
        performSegue(withIdentifier: "segue3", sender: self)
    case 8:
        performSegue(withIdentifier: "segue8", sender: self)
    default:
        break
    }
}

But actually, this method never get called, and the print out never show up when I select a row.

I did enable single selection, enable user interaction and set the delegate and datasource to the controller itself (That's automatically set when using UITableViewController, right?)

I am using static cells and default UITableViewCell. The view controller is UITableViewController.

Update:

I try to replace:

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

With:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

And this doesn't work also.

for Swift 3

override  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
    // your code ...  
}

and one thing make sure your tableView single selection property is selected ...

UITableViewController - iOS & Swift Tutorial, This tutorial demonstrates common use cases for the UITableView and UITableViewController classes. Xcode Tested with Xcode 11.3. Configure� UITableViewController. This tutorial demonstrates common use cases for the UITableView and UITableViewController classes.

Also check the selection of tableView in AttributeInspector of table view in your Storyboard. It should be selected to single selection.

How To: Working With Table View Controllers In Swift , The UITableViewController class itself is a subclass of UIViewController . Here's the class hierarchy of an example table view controller that� Overview. Table views on iOS display a single column of vertically scrolling content, divided into rows. Each row in the table contains one piece of your app’s content.

Since you are using a UITableViewController override the function. Otherwise if you are using a tableView in a ViewController make sure the delegate is set to your ViewController class.

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("You selected row #\(indexPath.row)!")
        switch indexPath.row {
        case 3:
            performSegue(withIdentifier: "segue3", sender: self)
        case 8:
            performSegue(withIdentifier: "segue8", sender: self)
        default:
            break
        }
    }

UITableViewController vs UIViewController + UITableView , UITableViewController. UITableViewController is a special UIViewController . Its view is a UITableView, can't change constraints. You can't add� UITableViewController() Default constructor that initializes a new instance of this class with no parameters. UITableViewController(IntPtr) A constructor used when creating managed representations of unmanaged objects; Called by the runtime.

Ok from what I’m seeing with your code. You is trying to print a string and also segue at the same time. But which object is you selecting to segue. It should be an array or something else example:

Var array["Swift", "IOS"]


DidSelect{

Switch: array[indexpath.row]
Case 3:
            Print(" u selected /(indexpath.row)")
     PerformSegue.........
Case 4 and so on.....

iOS UITableViewController in Swift using Xcode 8, We talk about how to implement a basic tableview in Swift 3 with sections in iOS 10 using Xcode Duration: 18:26 Posted: Sep 28, 2016 UITableViewController. You may have already noticed that there is also UITableViewController, but we haven’t used it in this tutorial. So what is an UITableViewController? It’s a component that inherits from UIViewController and that is already configured for using a table view: It has an UITableView and both the delegate and data source

UITableViewController in Xcode 9, iOS Interface Elements, UITableViewController. Learn How to use a Duration: 24:24 Posted: Sep 27, 2017 Then, type UITableViewController in the Subclass of field, and ContactsTableViewController in the Class field, and make sure to tick the checkbox for Also create XIB file, and click Next. Finally, save the file in the same folder as the AppDelegate .swift file, make sure the project is selected next to Targets , and click Create .

Introduction to the iOS UITableViewController, UITableViewController is a convenience class used to manage a table view. It does have some limitations, but as a beginner, it is a great place� The ViewController class implements UITableViewController. So there’s no need to implement the Delegate and DataSource protocols separately; The models array would contain the data for all the rows. The filteredModels array would contain the data for the rows that match the searched term based on the condition we’d be setting

UITableViewController Class (UIKit), A constructor that initializes the object from the data stored in the unarchiver object. UITableViewController(NSObjectFlag). Constructor to call on derived classes� You declare your class as a UITableViewController in this case the view outlet must be connected to the table view. However if you want a view that contains a UITableView then simple make the controller a UIViewController which conforms to the UITableViewDelegate and UITableViewDataSource protocols and this will go away.

Comments
  • Have you set the delegate?
  • What version of Xcode are using? While you say you are using a UITableViewController, you need to override the default implementation, which means you need the keyword override for the method. Your Update is not clear enough whether you put override or not.
  • Check the delegate is connected with the viewController
  • @OOPer Yes I should override that method.
  • Based on the answer you accepted, you are using Swift 2. You really should not be using that any more. You need to upgrade to Swift 3 since Swift 4 is now in beta.
  • The question clearly states they are using UITableViewController. You do not need to set the delegate or dataSource with UITableViewController.
  • Thanks! I should override that method. The tutorial I read is about UIViewController and I don't know the difference before.