4

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: enter image description here

I then add the table view component to the XIB: enter image description here

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:

enter image description here

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.

ScottOBot
  • 839
  • 3
  • 16
  • 37
  • Make sure you specified view controller class in Identity Inspector in interface builder. – JPetric Aug 15 '13 at 14:57
  • Not a direct answer to your question, but consider using a storyboard embed segue to have your [toolbar and table view](http://stackoverflow.com/questions/17825226/toolbar-between-uinavigationbar-and-uitableview/17825708#17825708) and stick with `UITableViewController` – Timothy Moose Aug 15 '13 at 15:03

3 Answers3

2

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];
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 2
    Hey thanks for the help I had `[[UIViewController alloc] initWithNibName:@"LeftMenuViewController" bundle:nil];` and I needed it to be `[[LeftMenuViewController alloc] initWithNibName:@"LeftMenuViewController" bundle:nil];` pretty much exactly what you said above thanks! @Rob – ScottOBot Aug 15 '13 at 15:36
1

Click on File's Owner in your NIB file in the Placeholders section and make sure the custom class is set to LeftMenuViewController or whatever you named it.

Chris Tetreault
  • 1,973
  • 13
  • 19
  • The class is set to LeftMenuViewController could it be something else? @codeInOrange – ScottOBot Aug 15 '13 at 15:05
  • @ScottOBot It should be the name of the subclass you created, LeftMenuViewController or LeftViewController, you mentioned both names – Chris Tetreault Aug 15 '13 at 15:07
  • Sorry it LeftViewController is a typo will edit! @codeInOrange – ScottOBot Aug 15 '13 at 15:11
  • @codeInOrange I think that file owner class setting in the NIB is used solely by IB when create outlets and actions, but the actual owner is determined at runtime when you instantiate the view controller and its NIB. But you're right that the problem rests with who the owner is. – Rob Aug 15 '13 at 15:34
0

In viewDidLoad:

self.tableView.delegate = self;
Abdullah Shafique
  • 6,878
  • 8
  • 35
  • 70
  • Omg I can't believe I forgot that! However, after adding a UITableView property to the .h and assigning the delegate to the controller as instructed above the app now throws a NSUnknownKeyException with the following error `[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key menuTable.`. What could be causing this? @Abdullah Shafique – ScottOBot Aug 15 '13 at 15:01
  • 1
    @ScottOBot If you properly instantiated your view controller, associating it with the NIB, you do not need to manually set the delegate like this if you set up the delegate and data source in the NIB to be "File Owner". The "File Owner" is configured when you instantiate your view controller. – Rob Aug 15 '13 at 15:11
  • I think it is an instantiation problem as mentioned by yourself in the above solution I'm testing out something and will post the solution! thanks for the help everyone! @Rob – ScottOBot Aug 15 '13 at 15:14