Alessandro Garcez
Alessandro Garcez

Reputation: 728

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

Upvotes: 9

Views: 17645

Answers (4)

Devendra Singh
Devendra Singh

Reputation: 777

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

Upvotes: 5

IronMan
IronMan

Reputation: 1505

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.

Upvotes: 22

stosha
stosha

Reputation: 2148

Try open xib-file and reconnect IBOutlet of the tableView. Hope that helps you.

Upvotes: 0

edzio27
edzio27

Reputation: 4140

Have you set method?:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

Upvotes: 0

Related Questions