tableView:numberOfRowsInSection:]: unrecognized selector sent to instance
Asked Answered
N

11

34

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

Nonbeliever answered 14/9, 2012 at 19:45 Comment(0)
N
33

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.

Necrology answered 14/9, 2012 at 20:15 Comment(2)
B
30

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.

Betz answered 18/11, 2013 at 1:18 Comment(1)
This great answer shows how to resolve it via the Connections Inspector, which is the equivalent to what @Necrology accomplishes via code.Wiener
C
22

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

class ClassName: UIViewController, UITableViewDelegate, UITableViewDataSource  
{

}
Carinacarinate answered 17/10, 2018 at 8:39 Comment(1)
That was my case! Thanks.Owner
S
11

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

Sundried answered 12/6, 2016 at 20:7 Comment(1)
That was the problem in my casePlanetstruck
B
8

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!

Bant answered 28/4, 2019 at 14:17 Comment(1)
surprised this saved me cause that seems obvious, but it made me realize that i had declared extensions and extended the wrong class when moving functions out of the class into an extension.Conglutinate
R
4

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.

Receive answered 29/7, 2013 at 21:18 Comment(0)
L
4

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.

Lineolate answered 17/9, 2018 at 12:42 Comment(0)
C
2
- (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.

Chiou answered 14/9, 2012 at 19:50 Comment(3)
- (NSInteger)numberOfRowsInSection:(NSInteger)section{ return 5; }Nonbeliever
Ahh, well that would have been helpful to put in the original question before you edited it ;)Chiou
The implementation for this method is not optional with UITableViewDataSource. Implementing this method resolved my crash.Joub
U
1

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.
Univocal answered 7/1, 2021 at 20:37 Comment(0)
C
0

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

Corrinnecorrival answered 1/8, 2018 at 15:24 Comment(1)
In the attached images you can see the outlets connected to the controller. Yours is a generic answer to a generic unrecognized selector question.Gruel
K
0

I was assigning TableviewDelegate and TableviewDataSource extension to another class

Changing it sorted my problem

Keenankeene answered 25/5, 2023 at 13:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.