ios tableView:numberOfRowsInSection:]: 无法识别的选择器发送到实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12431013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
tableView:numberOfRowsInSection:]: unrecognized selector sent to instance
提问by jimbob
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.
好的,所以我确保将 ViewController 设置为 dataSource 和 Delegate。我已将 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 是如何连接的:
回答by matt
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.
您已实施numberOfRowsInSection:
. 所以呢?那是错误的方法。这是无关紧要的。它永远不会被调用。
The method you need to implement is called tableView:numberOfRowsInSection:
. That's completely different.
您需要实现的方法称为tableView:numberOfRowsInSection:
。那是完全不同的。
回答by uplearnedu.com
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:
如果您单击该视图,它应该会在 INSPECTOR 中显示上述连接。如果您还没有连接 ViewController,您将收到错误消息:
'NSInvalidArgumentException', reason: '-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance
'NSInvalidArgumentException',原因:'-[UIView tableView:numberOfRowsInSection:]:无法识别的选择器发送到实例
How I stopped the error is you
我如何停止错误是你
- press on the ViewController to select it
- look for Referencing Outletsin the Connection inspectorto the right
- drag from the circle of the New Referencing Outletto the view
- when you release the mouse, a tiny popup will show - select datasource
- repeat step 3, and this time select delegate
- 按 ViewController 选择它
- 在右侧的连接检查器中查找引用插座
- 从新参考插座的圆圈拖动到视图
- 当您释放鼠标时,会显示一个小弹出窗口 - 选择数据源
- 重复第3步,这次选择delegate
When done, it should look like the image below!
完成后,它应该如下图所示!
This stopped the above error for me. I hope this helps someone.
这为我停止了上述错误。我希望这可以帮助别人。
回答by ninahadi
Check if you have UITableViewDelegate, UITableViewDataSource defined in your class file:
检查您的类文件中是否定义了 UITableViewDelegate、UITableViewDataSource:
class ClassName: UIViewController, UITableViewDelegate, UITableViewDataSource
{
}
回答by DonVaughn
回答by horkavlna
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;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
All other methods in UITableViewDelegate
are @optional
hence you don't need to implement them.
在其他所有方法UITableViewDelegate
都@optional
因此你不需要实现它们。
回答by user10375102
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.
如果您在视图控制器中正确连接了所有内容,但忘记扩展 UITableViewDelegate、UITableViewDataSource,您也会看到此错误。
回答by brynbodayle
- (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.
您没有实现 UITableViewDataSource 方法。删除声明,它就不会再崩溃了。或者为它提供一个方法体。
回答by Markus
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 UITableViewDelegateand UITableViewDataSourceprotocols
也许错误可能是,没有声明UITableViewDelegate和UITableViewDataSource协议
Keep it in mind, do not forget it!
记住它,不要忘记它!