2

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.

enter image description here

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.

9
  • Have you set the delegate? Commented Jul 17, 2017 at 2:56
  • 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. Commented Jul 17, 2017 at 3:01
  • Check the delegate is connected with the viewController Commented Jul 17, 2017 at 3:07
  • @OOPer Yes I should override that method. Commented Jul 17, 2017 at 3:12
  • 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. Commented Jul 17, 2017 at 3:19

4 Answers 4

5

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 ...

Sign up to request clarification or add additional context in comments.

1 Comment

The question clearly states they are using UITableViewController. You do not need to set the delegate or dataSource with UITableViewController.
4

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

Comments

2

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 } } 

1 Comment

Thanks! I should override that method. The tutorial I read is about UIViewController and I don't know the difference before.
0

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..... 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.