简体   繁体   English

tableView:numberOfRowsInSection:]: 无法识别的选择器发送到实例

[英]tableView:numberOfRowsInSection:]: unrecognized selector sent to instance

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.好的,所以我确保将 ViewController 设置为 dataSource 和 Delegate。 I have added the UITableViewDelegate and UITableViewDataSource to the <> thingies.我已将 UITableViewDelegate 和 UITableViewDataSource 添加到 <> 东西中。

I have set tableView.delegate = self and tableView.dataSource = self in my viewDidLoad method.我在 viewDidLoad 方法中设置了 tableView.delegate = self 和 tableView.dataSource = self 。

Here is my implementation of the viewDidLoad and viewDidAppear methods:这是我对 viewDidLoad 和 viewDidAppear 方法的实现:

- (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:这是我的 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:这是一个屏幕截图,显示了我的 tableView 是如何连接的:

在此处输入图片说明

Read what the error message is telling you!阅读错误消息告诉您的内容!

You have implemented numberOfRowsInSection: .您已实现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: .您需要实现的方法称为tableView:numberOfRowsInSection: That's completely different.那是完全不同的。

我将委托和数据源连接到 TableView

If you click on the view it should show the above connections in INSPECTOR.如果您单击该视图,它应该会在 INSPECTOR 中显示上述连接。 If you haven't got the ViewController hooked up as well you will get the error:如果您还没有连接 ViewController,您将收到错误消息:

'NSInvalidArgumentException', reason: '-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 'NSInvalidArgumentException',原因:'-[UIView tableView:numberOfRowsInSection:]:无法识别的选择器发送到实例

How I stopped the error is you我如何停止错误是你

  1. press on the ViewController to select it按 ViewController 选择它
  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重复第3步,这次选择delegate

When done, it should look like the image below!完成后,它应该如下图所示!

引用连接到视图的出口数据源和委托

This stopped the above error for me.这为我停止了上述错误。 I hope this helps someone.我希望这可以帮助别人。

Check if you have UITableViewDelegate, UITableViewDataSource defined in your class file:检查您的类文件中是否定义了 UITableViewDelegate、UITableViewDataSource:

class ClassName: UIViewController, UITableViewDelegate, UITableViewDataSource  
{

}

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:如果您在视图控制器中正确连接了所有内容,但忘记在故事板中分配自定义视图控制器类,您也会看到此错误:

在身份检查器中输入您的自定义视图控制器

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 [Fonts.THIRD tableView:numberOfRowsInSection:]: 无法识别的选择器发送到实例 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也许错误可能是,没有声明UITableViewDelegateUITableViewDataSource协议

Keep it in mind, do not forget it!记住它,不要忘记它!

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>我正在我的 ViewController 中实现没有 Storyboard 的 TableView,它使用两个协议子类化UIViewController<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:但是,如果您查看UITableViewDataSource则有两种方法是@required ,您需要在代码中实现这些方法:

- (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. UITableViewDelegate中的所有其他方法都是@optional因此您不需要实现它们。

如果您在视图控制器中正确连接了所有内容,但忘记扩展 UITableViewDelegate、UITableViewDataSource,您也会看到此错误。

- (NSInteger)numberOfRowsInSection:(NSInteger)section;

You are not implementing UITableViewDataSource method.您没有实现 UITableViewDataSource 方法。 Remove the declaration and it won't crash anymore.删除声明,它就不会再崩溃了。 Or alternatively provide a method body for it.或者为它提供一个方法体。

Another cause of this error I found (even when conforming to UITableViewDataSource !) was when setting a tableView.tableHeaderView before setting the tableView.dataSource .我发现这个错误的另一个原因(即使符合UITableViewDataSource !)是在设置tableView.tableHeaderView之前设置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 ):真正简单的解决方法是在设置tableHeaderView (或tableFooterView )之前先设置数据源:

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

If you have set up everything correctly and you still get this error.如果您已正确设置所有内容,但仍会收到此错误。 You will need to assign a controller to your custom class您需要为您的自定义类分配一个控制器在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 [UIViewController tableView:numberOfRowsInSection:]: 无法识别的选择器发送到实例 - [UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance -[UIViewController tableView:numberOfRowsInSection:]: 无法识别的选择器发送到实例 - -[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance tableview无法正常工作 - [UIViewController tableView:numberOfRowsInSection:]:无法识别的选择器发送到实例 - tableview not working - [UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance &#39;NSInvalidArgumentException&#39;-[UIViewController tableView:numberOfRowsInSection:]-无法识别的选择器已发送到实例 - 'NSInvalidArgumentException' - [UIViewController tableView:numberOfRowsInSection:] - unrecognized selector sent to instance 获取-[SearchViewController tableView:numberOfRowsInSection:]:无法识别的选择器已发送到实例 - Getting -[SearchViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance numberOfRowsInSection - 无法识别的选择器发送到实例问题 - numberOfRowsInSection - unrecognized selector sent to instance issue -[UIViewController tableView:numberOfRowsInSection:]:无法识别的选择器已发送到实例0x7ff437517770 - -[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7ff437517770 -[UIViewController tableView:numberOfRowsInSection:]:无法识别的选择器已发送到实例0x7fc748e37ea0&#39; - -[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fc748e37ea0' TableView在reloadData上崩溃 - &#39;无法识别的选择器发送到实例&#39; - TableView Crashes on reloadData - 'unrecognized selector sent to instance' 无法识别的选择器已发送到实例tableview swift - Unrecognized selector sent to instance tableview swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM