14

Can anyone see why in the world didSelectRowAtIndexPath would not be called? I have triple checked by delegate both in the code and in storyboard.

class AddCard: UIViewController,UIPopoverPresentationControllerDelegate, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var cardView: UIView! @IBOutlet weak var tableView: UITableView! let tableItems = ["Background Color","Background Image","Font Style","Font Color"] let cellID = "cell" override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func setBackgroundColor (_ color: UIColor) { cardView.backgroundColor = color } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return tableItems.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath as IndexPath) let row = indexPath.row cell.textLabel?.text = tableItems[row] return cell } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) { tableView.deselectRow(at: indexPath as IndexPath, animated: true) print(indexPath.row) let row = indexPath.row switch(row){ case 0: let popoverVC = storyboard?.instantiateViewController(withIdentifier: "colorPickerVC") as! ColorPickerViewController popoverVC.modalPresentationStyle = .popover popoverVC.preferredContentSize = CGSize(width: 284, height: 446) if let popoverController = popoverVC.popoverPresentationController { popoverController.sourceView = self.view popoverController.sourceRect = CGRect(x: 0, y: 0, width: 85, height: 30) popoverController.permittedArrowDirections = .any popoverController.delegate = self popoverVC.delegate = self } present(popoverVC, animated: true, completion: nil) break default: break } } } 
2
  • 4
    You didn't update the signature of didSelectRowAtIndexPath: to the Swift 3 one. From the doc: optional func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath), notice the _ the didSelectRowAt vs didSelectRowAtIndexPath, like the other one you updated, but not this one. Remove the line and let XCode do the autocompletion. Else, you can just replace it with the one from the doc. Commented Nov 8, 2016 at 16:35
  • Killed it! Thanks man. Commented Nov 8, 2016 at 16:36

1 Answer 1

19

Swift 3 modified the signature of the method (a lot of methods too, new "rules"/style)

Replace:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) with
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

Notice the _, the didSelectRowAt vs didSelectRowAtIndexPath, like the other ones you updated (which adapted also the same "style"), but not this one.

Remove the line and let XCode do the autocompletion. Else, you can just replace it with the one from the doc.

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

1 Comment

Thanks! And why on earth.... is it that when they decide to rename everything in each new version of swift, that the xcode editor does not suggest the new names when you type in a deprecated name. I'm just learning xcode and swift, and I am chocked how bad a developer experience this is.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.