63

My application works for iOS 5.1 but for iOS 6 simulator I get the following error.

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "MainListViewController" nib but didn't get a UITableView.'

I am subclassing UITableViewController and I don't want to change it.

The tableview is created programmatically, there is a dummy MainListViewController.xib to load from Mainwindow.xib Tab Bar Controller.

I also tried to delete MainListViewController.xib, remove it from the MainWindow.xib Tab Bar Controller, created the MainListViewController in AppDelegate and added it to Tab Bar Controller as UITabBarItem to get rid of this nib problem, but I still get the same error.

mattjgalloway
  • 34,792
  • 12
  • 100
  • 110
irmakoz
  • 1,166
  • 2
  • 12
  • 21
  • Where is your table being loaded? And is it being set to the view outlet/property – CodaFi Jun 27 '12 at 08:03
  • It is loaded in viewDidLoad, in the second version(which adds a UITabBarItem programmatically) it is not set to a outlet/property. – irmakoz Jun 27 '12 at 08:50

13 Answers13

103

If you have a NIB for the UITableViewController subclass then its view outlet must be hooked up to a UITableView.

You're right to delete MainListViewController.xib and do it all in code, but the reason it didn't work for you is because the old XIB will not be deleted when you build & run. So, delete the app from the simulator and try again. It should work then.

mattjgalloway
  • 34,792
  • 12
  • 100
  • 110
  • 11
    Thanks! I deleted the old application,cleaned the project and closed the iOS simulator now the version without the .xib file works. – irmakoz Jun 27 '12 at 09:07
  • I don't understand, if the xib file is not used anywhere to initialize the subclass of UITableViewController, why would it be a problem at all? It sounds like a bug of Xcode or something... anyway, i deleted the xib file, cleaned the project and it didn't work, but after i deleted the old app then it worked. – xu huanze Feb 19 '13 at 02:25
  • 1
    @xuhuanze - But that's the point of this question - the NIB was being used to initialise a subclass of `UITableViewController`. If yours was something different, then it's not this question you should be looking at. – mattjgalloway Feb 19 '13 at 20:00
  • @mattjgalloway you are right. my issue is different, i created the controller with a xib, but later i decided not to use it but same error... – xu huanze Feb 20 '13 at 07:02
  • How in the world you knew the ridiculous answer to this (particularly the 2nd part), is amazing. – jungledev Dec 14 '16 at 04:05
29

I had a similar problem using storyboards. I'll post my solution for the benefit of others. The key is that if you have correctly set the file's owner to the subclass of UITableView you still have to make sure the view property is set to the Table View. I'm using storyboards, but the same sort of thing should apply to nibs as well. Expand the "document outline" so you can see the hierarchy of your storyboard. I will include some screen shots below. The quick way to solve this is that you want to look at your view in the document outline. Delete any tableview you have as a child of it. Drag the other tableview on top of the view property. Done. I'll show you in screen shots.

enter image description here

Notice the two tables. You want to remove table1 and its cell.

enter image description here

Good. Now drag table2 to the view and you're done. Should work now. Basically if you have a subclass of UITableViewController then it must have Table View in place of view or it will crash.

enter image description here

SmileBot
  • 19,393
  • 7
  • 65
  • 62
  • Ran into this when I wanted to get a statically sized view above the tableview, which seems impossible (no resizing of top view) when subclassing UITableViewController. Your answer convinced me I had to subclass UIViewController instead. Then I hooked up my own UITableView instance. Thanks! – owenfi May 26 '14 at 21:11
  • @owenfi If you do need a tableViewController you can always use an embedded one. So, if you're doing a static tableview it must be a tableViewController. Here you can get around the issues by using an embedded tableViewController. I use them all the time. – SmileBot May 26 '14 at 23:29
  • @cocoanut sorry, I needed a "static sized" scrollview (horizontally scrolling) atop a dynamic prototype tableview. Not sure if I follow your example, are you able to extend UITableViewController and resize it's tableView to take, say the bottom 50% of the screen? – owenfi May 27 '14 at 02:16
  • @owenfi I'm not sure exactly what you're trying to achieve. Perhaps check out http://www.raywenderlich.com/4680/how-to-make-an-interface-with-horizontal-tables-like-the-pulse-news-app-part-1 , but you might also consider using UICollectionView if tableView doesn't suite your needs. I think that tutorial is obsolete because collectionViews are used in place of this functionality in modern versions of the sdk. – SmileBot May 27 '14 at 14:37
  • 1
    I didn't understand this answer at first, so to clarify for others: the `Table View` must be a child of the `ActivitiesTVC - Activities` node and NOT a child of the `View` – GraehamF Jun 29 '15 at 01:00
17

The reason might be :

.h file check that it is a subclass of UIViewController. it will solve the problem.

Abdul Yasin
  • 3,480
  • 1
  • 28
  • 42
12

In short, you are setting a custom UITableViewController subclass to a UIViewController !! You cannot do this. You should give the UIViewController a UIViewController subclass.

dev4life
  • 10,785
  • 6
  • 60
  • 73
12

If you use UITableView as your Top View.

Like this :

enter image description here

You need use UITableViewController in your Controller

class ItemsViewController: UITableViewController

If you use UITableView under a View in your Storyboatd.

Like this :

enter image description here

You need use UITableViewDelegate and UITableViewDataSource with UIViewController

class ItemsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
saneryee
  • 3,239
  • 31
  • 22
4

I find the solution when using Storyboards and 1 table View.

The key is when you create a custom class (newViewController) just check that is a subclass of UITableViewController. Once created Go to the table view controller (of our table) and in the Identity Inspector select the custom class that just created before (newViewController).

That works for me. Hope my comment help someone.

chadenis
  • 71
  • 8
3

There is several reason why this may occur.

  1. You may have not have added UITableViewDelegate & UITableViewDataSource to your .h

    @interface TableController : UIViewController {

  2. You may have not connected your declared outlets to the components in the Storyboard/Nib

  3. You maybe calling a UIViewController instead of a UITableView or vide verser in your .h

@interface TableController : UIViewController

or

 @interface TableController : UITableViewController 
Tash Pemhiwa
  • 7,590
  • 4
  • 45
  • 49
Joe Barbour
  • 842
  • 9
  • 14
3

Please connect your table view as a View outlet.outlet must be hooked up to a UITableView.

1

I had this problem and solving it was just one simple thing.

Keep the nib if you already created ...

Go to (XIB file) and add a tableView component from objects library to the XIB file's iPhone screen, and it should work :)

Mandar
  • 116
  • 1
  • 5
DevTorch
  • 131
  • 1
  • 6
1

@smileBot answer helped me realized the solution is very easy and the error raised by iOS makes sense

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "MainListViewController" nib but didn't get a UITableView.'

Their basically saying that nib's view controller is not returning a UITableView because it isn't, for most - your nib is returning a View - which is created for you by default when you create a new ViewController in the Xcode IDE.

The solution is as simple as deleting that View - which might feel against the norm - but go ahead and select it and backspace - then drag and drop a UITableView inside your ViewController and this UITableView will now act as your root view inside your TableViewController.

Keep in mind this is only necessary for classes directly subclassing UITableViewController.

Good luck.

Ryhan
  • 1,815
  • 1
  • 18
  • 22
0

This worked for me. Hope it helps you.

In AppDelegate-->

#import "TableViewController.h" // name of your TableViewController class

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//PUT
TableViewController *controller = [[TableViewController alloc]
                                  initWithStyle:UITableViewStylePlain];
    self.window = [[UIWindow alloc]
                   initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = controller;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    // Override point for customization after application launch.
    return YES;
}

Make sure in storyboard your view controller points to this class:

Story Board

uplearned.com
  • 3,393
  • 5
  • 44
  • 59
0

I also encountered this problem, but I used storyboard in xcode 6.2. I deleted the corresponding view from storyboard and re-dragged the table view controller from the object library to storyboard, then it works.

Mengjie
  • 11
  • 2
0

I have same problem and after waste 3 hours i find it out..."this is mistake" i use tableviewcontroller class and give uiviewcontroller reference of this class, instead of view controller class i take tableviewcontroller class as i take uiviewcontroller in storyboard. so delete tableviewcontroller class and paste your code of tableviewcontroller class to view controller class,"not paste whole code only paste logic" after this in storyboard in identity inspector give class reference of current view controller class.