jim*_*bob 27 xcode delegates uitableview ios
我有一个奇怪的问题.我收到此错误:
-[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)
Run Code Online (Sandbox Code Playgroud)
好的,我确保将ViewController设置为dataSource和Delegate.我已将UITableViewDelegate和UITableViewDataSource添加到<> thingies中.
我在viewDidLoad方法中设置了tableView.delegate = self和tableView.dataSource = self.
这是我对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];
}
Run Code Online (Sandbox Code Playgroud)
这是我的numberOfRowsInSection方法体:
- (NSInteger)numberOfRowsInSection:(NSInteger)section{
return 5;
}
Run Code Online (Sandbox Code Playgroud)
这是我的视图控制器的头文件:
#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
Run Code Online (Sandbox Code Playgroud)
这是一个屏幕截图,显示我的tableView如何连接:
mat*_*att 32
阅读错误消息告诉您的内容!
你已经实施了numberOfRowsInSection:
.所以呢?那是错误的方法.这是无关紧要的.永远不会被召唤.
调用您需要实现的方法tableView:numberOfRowsInSection:
.那是完全不同的.
upl*_*com 27
如果单击视图,它应在INSPECTOR中显示上述连接.如果您还没有连接ViewController,您将收到错误:
'NSInvalidArgumentException',原因:' - [UIView tableView:numberOfRowsInSection:]:发送到实例的无法识别的选择器
我是如何阻止错误的
完成后,它应该如下图所示!
这为我停止了上述错误.我希望这可以帮助别人.
小智 14
检查是否在类文件中定义了UITableViewDelegate,UITableViewDataSource:
class ClassName: UIViewController, UITableViewDelegate, UITableViewDataSource
{
}
Run Code Online (Sandbox Code Playgroud)
我认为展示这个答案很重要。这看起来很傻,可能是这样,但我失去了很长一段时间,直到我看到了错误。
我创建了一个新视图并复制了部分代码。
我创建了新视图:
class THIRD: UIViewController {
[...]
}
Run Code Online (Sandbox Code Playgroud)
我开始复制代码:
func numberOfSections(in tableView: UITableView) -> Int {
...
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
...
}
Run Code Online (Sandbox Code Playgroud)
等等。
然后,消息错误:
[Fonts.THIRD tableView:numberOfRowsInSection:]: 无法识别的选择器发送到实例 0x10204b200
我一次又一次地审查了故事板。我一遍又一遍地检查代码。我失去了一个美好的时光。
终于明白了!!!
我忘了声明:
class THIRD: UIViewController, UITableViewDelegate, UITableViewDataSource {
Run Code Online (Sandbox Code Playgroud)
也许错误可能是,没有声明UITableViewDelegate和UITableViewDataSource协议
记住它,不要忘记它!
归档时间: |
|
查看次数: |
52632 次 |
最近记录: |