Error connecting tableView to data source in .XIB

So, I'm trying to connect the tableView component to a UIViewController created through the .xib user interface. I get the classic -[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x1f544cb0 and guess that I have connected something incorrectly or am doing something that I should not do with the interface builder. I searched google and stack overflow and found similar questions, but not with answers that helped my situation. I hit my head trying to figure it out and hope someone here can help. I only did this through a storyboard or with .xib when the controller was a UITableViewController. I usually just use the UITableViewController, but I may need to add a toolbar at the bottom of the view so that this option is not viable. Here are the steps I took to create the class, please let me know if I am doing something wrong.

First I create a XIB file for the user interface: enter image description here

Then I add the table view component in XIB: enter image description here

In LeftMenuViewController.h, I add the following line of code to tell the class to use UITableViewDelegate and UITableViewDataSource:

@interface LeftMenuViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

Then I connect the table view component with the delegate and data source, dragging the control from the component to the File Owner header and selecting the delegate and data source, as you can see:

enter image description here

Then I add the following lines of code to LeftMenuViewController.m to provide definitions for the necessary 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 application to throw an NSInvalidArgumentException that throws an error [UIViewController tableView:numberOfRowsInSection:]: unrecognized selector . What am I doing wrong? Everything I tried or recommended did not work.

+4
source share
3 answers

The fact that the error refers to the UIViewController in your error message, [UIViewController tableView:numberOfRowsInSection:]: unrecognized selector , means that the file owner has not been set (it must refer to your subclass of the view class, not the UIViewController itself). You correctly specified the delegate and data source in your NIB, so I wonder how you created the instance of your view controller.

You should have created an instance with something like:

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

Or, if the NIB name does not match the controller name:

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

Click "Owner File" in your NIB file under "Placeholders" and make sure that the custom class is set to LeftMenuViewController or LeftMenuViewController you called it.

+1
source

In viewDidLoad:

 self.tableView.delegate = self; 
0
source

Source: https://habr.com/ru/post/1497261/


All Articles