0

I'm trying to convert my UITableViewController to a UIViewController with a UITableView element. I changed the superclass to UIViewController, added the UITableViewDelegate, added a local UITableView (which I linked to my table view in storyboard), and set tableView.delegate = self. Now I'm trying to override the tableView() methods so I can populate my cells. However, it's not letting me do this - "Method does not override any method from its superclass". Here's the relevant code:

class MenuViewController: UIViewController, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.title = "Click Item"

    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "menuItemCell")
    tableView.delegate = self
}

Is there something I'm missing? I checked similar stackoverflow questions and this is what they said to do. I'm pretty new at all this so I could just be overlooking something.

1 Answer 1

4

Since you are no longer writing a subclass of UITableViewController you no longer override its delegate and datasource functions, but you are providing your own implementation to conform to those protocols. Swift strictly checks if the method you are declaring to override is in fact already implemented in the base class. This was true with the UITableViewController, but it's no longer the case with UIViewController.
In short: drop the override keyword from the delegate and dataSource functions.

7
  • That's what I thought originally, but one answer (a comment actually) that I read on here said otherwise. It makes sense that it shouldn't be able to override. Only reason I thought I was wrong was because the implementation wasn't yielding correct results, but now I can narrow that down to an implementation error rather than semantics. Thanks for clearing it up! EDIT: On second thought, I set a breakpoint for one of the tableView( ) methods and it doesn't actually get called. This leads me to believe they are still not linked. Any ideas?
    – Niko
    Aug 4, 2015 at 16:01
  • If you are not seeing the correct result, make sure that you are also setting the dataSource. In the code above you only set the delegate.
    – andreamazz
    Aug 4, 2015 at 16:03
  • Perfect, that did it. Thanks! If you don't mind me asking, what role does the UITableViewDataSource play in this? I understand that the delegate gives you access to table methods (I think), but I don't see what the DataSource protocol is doing.
    – Niko
    Aug 4, 2015 at 16:05
  • The delegate basically tells the table what to do, while the data source tells the table what to display. You can Command-click the UITableViewDelegate and UITableViewDataSource to see the list of methods that you can implement.
    – andreamazz
    Aug 4, 2015 at 16:07
  • Okay makes sense. Should I be implementing any DataSource methods? Cause I didn't do that originally when my controller was a UITableViewController and it worked just fine.
    – Niko
    Aug 4, 2015 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.