Issue Connecting tableView to Data Source in .XIB

ios, uitableview, xcode, xib

Solution

The fact that the error is referencing `UIViewController` in your error message, `[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector`, that means that the file owner has not been set (it should be referencing your view controller subclass, not `UIViewController`, itself). You've correctly specified the delegate and data source in your NIB, so I wonder how you instantiated your view controller.

You should have instantiated it with something like:

SecondViewController *controller = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:controller animated:YES completion:nil];

Or, if the NIB name doesn't match the name of the controller:

SecondViewController *controller = [[SecondViewController alloc] initWithNibName:@"MyNibName" bundle:nil];
[self presentViewController:controller animated:YES completion:nil];

Problem

So I'm trying to connect a tableView component to a UIViewController created through the .xib user interface. I am getting the classic `-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x1f544cb0` and am guessing that I have connected something incorrectly or am doing something that I shouldn't be doing with the interface builder. I have searched google and stack overflow and have found similar questions but non with answers that have helped my situation. I have been hitting my head trying to figure this out and hope someone here can help. I have only ever done this through storyboard or with .xib when the controller was a UITableViewController. Usually I would just use a UITableViewController but I may need to add a toolbar on the bottom of the view so that option is not viable. Here are the steps I took to create the class please let me know if I am doing something wrong I first create the file with an XIB for user interface: I then add the table view component to the XIB: In the LeftMenuViewController.h I add the following line of code to tell the allow for the class to be used as a UITableViewDelegate and UITableViewDataSource: `@interface LeftMenuViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>` I then connect the table view component up to the delegate and data source by control dragging from the component to the File's Owner heading and selecting the delegate and data source as can be seen: I then add the following lines of code to LeftMenuViewController.m to provide definitions for the required TableView delegate methods: ``` - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 5; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 48; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell; return cell; } ``` This process causes the app to throw a NSInvalidArgumentException which produces the `[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector` error. What am I doing wrong? Everything I have tried or have been recommended hasn't worked.

Original source