Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tableview not working - [UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance

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:

  1. Add UITableViewDelegate and UITableViewDataSource in my viewController.
  2. Insert the tableview into a view in my viewController.xib.
  3. Create 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).
  4. Implement the methods - (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
like image 335
Alessandro Garcez Avatar asked Jun 16 '13 14:06

Alessandro Garcez


2 Answers

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.

like image 60
IronMan Avatar answered Oct 21 '22 13:10

IronMan


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

enter image description here

Correct Image

enter image description here

like image 30
Devendra Singh Avatar answered Oct 21 '22 14:10

Devendra Singh