4

I have this button in my viewController and when I press on it, it should go to the TableViewController. When I do this the app crashes and prints this error in the console. Can someone tell me what Im doing wrong? Thank you!

Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: '-[UITableViewController loadView] instantiated view controller with identifier "UIViewController-iLh-Fe-Ezq" from storyboard "Main", but didn't get a UITableView.'

Derlin
  • 9,572
  • 2
  • 32
  • 53
coding22
  • 689
  • 1
  • 7
  • 28
  • 1
    the top-level object in the scene must be a tableview. My guess is that you have a tableview inside a UIview – Paulw11 Oct 17 '15 at 01:25
  • This problem may occurs when your view controller in storyboard is UITableViewController but your custom class for that view controller is not, and vice versa. – kientux Oct 17 '15 at 01:57
  • So do I have to make a table view controller without the view controller? – coding22 Oct 17 '15 at 03:45
  • Im trying to make a table view but I need to add a bar button item, how do I do that? Its not letting me put it above the prototype cell. – coding22 Oct 17 '15 at 08:05

2 Answers2

17

When I got this error, I had initially used the boilerplate code for the class UITableViewController, but the actual view controller was a UIViewController.

Original code (resulting in the error):

Note that this is connected to a UIViewController in Storyboard.

class MainViewController: UITableViewController {
//insert rest of code here 
//note that funcs such as numberOfRowsInSection(_:) will have the override keyword 
}

Working code (removing the error):

class MainViewController: UIViewController {
//insert code here 
//you also must *remove the override keywords* for
// some of the included functions or you will get errors
}

Remember also to reference an IBOutlet for your UITableView in your view controller, and set the delegate and datasource (in Storyboard, you Ctrl+Drag from the UITableView to the yellow circle at the top of the view controller, and click dataSource. Repeat this for the delegate as well).

David Liu
  • 316
  • 3
  • 7
  • what do you meant by "click dataSource" after drag a component segue line to that yellow circle? – Janaka R Rajapaksha Jun 19 '16 at 14:26
  • @JanakaRRajapaksha apologies but I wrote this half a year ago so I actually cannot remember what I was referring to. It is likely that I meant that "dataSource" was one of the options in the menu that pops up after Ctrl+Dragging, however. – David Liu Jul 20 '16 at 07:54
  • 1
    @JanakaRRajapaksha, it seems when ctrl-dragging from UITableView to the yellow circle like David said, you get a menu with the choices datasource & delegate. Pick one & then ctrl-drag again to pick the other as well. In addition to taking out the "overrides" from func certain funcs, I also changed the class definition to "class MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { ... }". Thx for solution, David. – rockhammer Sep 10 '16 at 08:42
  • 1
    Thanks for the solution, was exactly my set up, completely forgot that my view was a UIViewController.. – Pierre Sep 13 '16 at 09:25
1

I got it to work....I used a table view controller without the view controller and embedded a navigation controller.

coding22
  • 689
  • 1
  • 7
  • 28