33
votes

I have a weird problem. I get this error:

 -[FourSquareCheckInViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7aecc0
2012-09-14 19:18:39.039 [5869:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FourSquareCheckInViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7aecc0'
*** First throw call stack:
(0x3545e88f 0x37805259 0x35461a9b 0x35460a83 0x353bb650 0x32ee3693 0x32ee49ed 0x32ee493f 0x32ee451b 0x32ef17df 0x32ef16af 0x32e95f37 0x353bd1fb 0x3228daa5 0x3228d6bd 0x32291843 0x3229157f 0x322894b9 0x35432b1b 0x35430d57 0x354310b1 0x353b44a5 0x353b436d 0x37050439 0x32ec0cd5 0xb7ebb 0xb7e60)
terminate called throwing an exception(lldb) 

Ok so I have made sure that the ViewController is set as the dataSource and the Delegate. I have added the UITableViewDelegate and UITableViewDataSource to the <> thingies.

I have set tableView.delegate = self and tableView.dataSource = self in my viewDidLoad method.

Here is my implementation of the viewDidLoad and viewDidAppear methods:

- (void)viewWillAppear:(BOOL)animated{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer; // 1km
    [locationManager startUpdatingLocation];
    locationLat = [locationManager location].coordinate.latitude;
    locationLng = [locationManager location].coordinate.longitude;
    [locationManager stopUpdatingLocation];

    // 1
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = locationLat;
    zoomLocation.longitude= locationLng;
    // 2
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
    // 3
    MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];                
    // 4
    [_mapView setRegion:adjustedRegion animated:YES]; 
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    _tableView.delegate = self;
    _tableView.dataSource = self;

    // Do any additional setup after loading the view.
    // 1
    MKCoordinateRegion mapRegion = [_mapView region];
    CLLocationCoordinate2D centerLocation = mapRegion.center;

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer; // 1km
    [locationManager startUpdatingLocation];
    locationLat = [locationManager location].coordinate.latitude;
    locationLng = [locationManager location].coordinate.longitude;
    [locationManager stopUpdatingLocation];

}

Here is my method body for numberOfRowsInSection:

- (NSInteger)numberOfRowsInSection:(NSInteger)section{
    return 5;
}

Here is my header file for the view controller:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

#define METERS_PER_MILE 1609.344

@interface FourSquareCheckInViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, CLLocationManagerDelegate, MKMapViewDelegate>{

}


@property (weak, nonatomic) IBOutlet MKMapView *_mapView;

- (NSInteger)numberOfRowsInSection:(NSInteger)section;
@property (weak, nonatomic) IBOutlet UITableView *_tableView;



@end

Here is a screenshot showing how my tableView is hooked up:

enter image description here

10

10 Answers

33
votes

Read what the error message is telling you!

You have implemented numberOfRowsInSection:. So what? That's the wrong method. It is irrelevant. It will never be called.

The method you need to implement is called tableView:numberOfRowsInSection:. That's completely different.

28
votes

I had delegate and datasource hooked up to the TableView

If you click on the view it should show the above connections in INSPECTOR. If you haven't got the ViewController hooked up as well you will get the error:

'NSInvalidArgumentException', reason: '-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance

How I stopped the error is you

  1. press on the ViewController to select it
  2. look for Referencing Outlets in the Connection inspector to the right
  3. drag from the circle of the New Referencing Outlet to the view
  4. when you release the mouse, a tiny popup will show - select datasource
  5. repeat step 3, and this time select delegate

When done, it should look like the image below!

Referencing outlet Datasource and delegate connected to view

This stopped the above error for me. I hope this helps someone.

20
votes

Check if you have UITableViewDelegate, UITableViewDataSource defined in your class file:

class ClassName: UIViewController, UITableViewDelegate, UITableViewDataSource  
{

}
11
votes

You'll also see this error if, although you've hooked everything up properly in the view controller, you have forgotten to assign your custom view controller class in your storyboard:

Enter your custom view controller in identity inspector

6
votes

I think it's important that I show this answer. It seems silly, probably it is, but I lost a good while until I saw the error.

It happened to me creating a new view and copying part of the code.

I created the new view:

class THIRD: UIViewController {

[...]

}

And I started copying the code:

func numberOfSections(in tableView: UITableView) -> Int {
...
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
...
}

Etc.

And then, the message error:

[Fonts.THIRD tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x10204b200

I reviewed the Storyboard again and again. And I checked the code again and again. And I lost a good time.

Finally I realized!!!

I forgot to declare:

class THIRD: UIViewController, UITableViewDelegate, UITableViewDataSource {

Perhaps the error may be, do not declare the UITableViewDelegate and UITableViewDataSource protocols

Keep it in mind, do not forget it!

4
votes

It is very simple, I had got a similar problem. I am implementing my TableView without Storyboard in my ViewController which has subclassed UIViewController with two protocols: <UITableViewDataSource, UITableViewDelegate>

But if you look to the UITableViewDataSource there are two methods witch are @required and these you need to implement in your code:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

All other methods in UITableViewDelegate are @optional hence you don't need to implement them.

4
votes

You'll also see this error if, although you've hooked everything up properly in the view controller, you have forgotten to extend UITableViewDelegate,UITableViewDataSource.

2
votes
- (NSInteger)numberOfRowsInSection:(NSInteger)section;

You are not implementing UITableViewDataSource method. Remove the declaration and it won't crash anymore. Or alternatively provide a method body for it.

1
votes

Another cause of this error I found (even when conforming to UITableViewDataSource!) was when setting a tableView.tableHeaderView before setting the tableView.dataSource.

// Header config
self.tableView.tableHeaderView = header; // CRASH!! Triggers table view update and thus calls unknown selector.
self.tableView.dataSource = self;

Really simple fix is to set the data source first before setting the tableHeaderView (or tableFooterView):

self.tableView.dataSource = self;
// Header config
self.tableView.tableHeaderView = header; // Runtime can now find selector even though it is already defined.
0
votes

If you have set up everything correctly and you still get this error. You will need to assign a controller to your custom class enter image description here