I'm trying to init a uitableview
using xib
but when I run the app in simulator, the below exception is thrown.
2013-06-16 10:40:48.552 CoreDataExample[60661:c07] -[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x81765a0
2013-06-16 10:40:48.554 CoreDataExample[60661:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x81765a0'
Below the steps that i have followed trying to start the tableview:
UITableViewDelegate
and UITableViewDataSource
in my viewController
.tableview
into a view in my viewController.xib
.datasource
and delegate
in it files's owner (pressing control key and draging the mouse's arrow from component to file's owner and selecting the option delegate for example).- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
and - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
in my viewController.m
.Below the implemetation of two methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = nil;
static NSString *identifier = @"identifier";
cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
cell.textLabel.text = @"Olá";
cell.detailTextLabel.text = @"Subtitle" ;
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
return cell;
}
ViewController.h below:
@interface CDEMainViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
@end
In XIB, Click on the File Owner, Check the Class Type. I guess it is UIViewController.
If it is UIViewController
change it to CDEMainViewController
. It will work properly.
In your ViewController XIB, Right click on the Tableview, Check the tableview datasource and delegate may be connected with view, not with File Owner, Disconnect these and connect again tableview datasource and delegate with File Owner, This resolved my issue. Please check screen shots also for refrence.
InCorrect Image
Correct Image
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With