Reputation: 21
I'm busy making an app that loads a bunch of names from a .plist. I've set it up so that the .plist gets loaded into an UiTableView. Now the problem is that I get an error code whenever I try to open the menu that has the Table with the names in it.
This is the error message:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fc748e37ea0'
This is my viewController.m
#import "ViewController.h"
#import "Stuff.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Stuff *s = [[Stuff alloc] init];
[s getStuff];
self.items = [[NSMutableArray alloc] initWithArray:s.stuff];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.items count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath*)indexPath
{
NSString *id = @"plistdata";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:id forIndexPath:indexPath];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:id];
}
cell.textLabel.text = self.items[indexPath.row];
return cell;
}
@end
Thanks!
Upvotes: 1
Views: 8273
Reputation: 131398
To build on what Matt said: The error you are getting says that the UIViewController class (the base class of all view controllers) does not recognize the message tableView:numberOfRowsInSection:
Somehow when you're creating a view controller you're creating a generic UIViewController object instead of an instance of your custom ViewController class.
The reason for this depends on how You're creating your view controller.
If you create it using code with a call to -[UIViewController initWithNibName:bundle:]
, you may be creating an instance of UIViewController rather than your custom ViewController class:
ViewController *myVC = [[UIViewController alloc] initWithNibName: @ViewController"
bundle: nil];
If you're creating it using a storyboard, you may have the storyboard configured incorrectly.
To help you figure out why you are getting a generic UIViewController instead of your custom class you'll need to tell us how you are creating an instance of your custom view controller.
Upvotes: 5
Reputation: 2495
Verify that the File's Owner object in interface builder is set to be an instance of the UITableViewController subclass that you're implementing. Also make sure that the tableView outlet is linked to your table view.
.h file
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
Storyboard Setup
Attributes Inspector:
Connections Inspector:
Upvotes: 0
Reputation: 534893
The problem is that the code you have shown is never being called, because the view controller you've shown (ViewController) is not the table view's data source / delegate. The data source / delegate messages are being sent to some other view controller. (This is probably because the storyboard is misconfigured, but you haven't given enough information for me to be sure of that.)
Upvotes: 2