33

奇妙な問題があります。次のエラーが表示されます。

 -[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) 

わかりましたので、ViewController が dataSource および Delegate として設定されていることを確認しました。<> に UITableViewDelegate と UITableViewDataSource を追加しました。

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];

}

numberOfRowsInSection のメソッド本体は次のとおりです。

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

ビューコントローラーのヘッダーファイルは次のとおりです。

#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

これは、私のtableViewがどのように接続されているかを示すスクリーンショットです:

ここに画像の説明を入力

4

10 に答える 10

33

エラーメッセージの意味を読んでください!

を実装しnumberOfRowsInSection:ました。だから何?それは間違った方法です。それは無関係です。呼び出されることはありません。

実装する必要があるメソッドは と呼ばれtableView:numberOfRowsInSection:ます。それはまったく違います。

于 2012-09-14T20:15:41.243 に答える
29

デリゲートとデータソースをTableViewに接続しました

ビューをクリックすると、INSPECTOR に上記の接続が表示されます。ViewController も接続していない場合は、次のエラーが発生します。

'NSInvalidArgumentException'、理由: '-[UIView tableView:numberOfRowsInSection:]: 認識されないセレクターがインスタンスに送信されました

エラーを止めた方法はあなたです

  1. ViewController を押して選択します
  2. 右側の接続インスペクタで参照アウトレットを探します
  3. 新しい参照アウトレットの円からビューにドラッグします
  4. マウスを放すと、小さなポップアップが表示されます -データソースを選択してください
  5. 手順 3 を繰り返し、今度はデリゲートを選択します

完了すると、下の画像のようになります。

ビューに接続されたアウトレット データソースとデリゲートを参照する

これにより、上記のエラーが停止しました。これが誰かに役立つことを願っています。

于 2013-11-18T01:18:51.327 に答える
11

View Controller ですべてを適切に接続したにもかかわらず、ストーリーボードでカスタム View Controller クラスを割り当てるのを忘れた場合にも、このエラーが表示されます。

カスタム ビュー コントローラーを ID インスペクターに入力します。

于 2016-06-12T20:07:45.600 に答える
2
- (NSInteger)numberOfRowsInSection:(NSInteger)section;

UITableViewDataSource メソッドを実装していません。宣言を削除すると、クラッシュしなくなります。または、代わりにメソッド本体を提供します。

于 2012-09-14T19:50:23.020 に答える
1

UITableViewDataSource私が見つけたこのエラーの別の原因は ( !に準拠している場合でも)、 を設定するtableView.tableHeaderView前に a を設定したときでしたtableView.dataSource

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

tableHeaderView本当に簡単な修正は、 (または)を設定する前に最初にデータソースを設定することですtableFooterView

self.tableView.dataSource = self;
// Header config
self.tableView.tableHeaderView = header; // Runtime can now find selector even though it is already defined.
于 2021-01-07T20:37:33.350 に答える