I’m trying to learn table views and I’ve hit a snag. I have the view delegate and datasource connected correctly in Storyboard, but I get the following runtime error when I get to the section of my app containing the table view.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationItem tableView:numberOfRowsInSection:]: unrecognized selector sent to instance
Here’s the chunk from my implementation file
@implementation CraftingViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = @"Detail";
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
That error message is displaying because
tableView:numberOfRowsInSection:is being sent to an object of typeUINavigationItemwhile it seems like yourCraftingViewControllerclass is probably of typeUITableViewController. I would make sure that you have connected your delegate to the correct class, because it doesn’t seem likeCraftingViewControlleris connected properly.