web-dev-qa-db-ja.com

tableView:numberOfRowsInSection:]:インスタンスに送信された認識されないセレクター

奇妙な問題があります。私はこのエラーを受け取ります:

 -[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;
}

これが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

TableViewの接続方法を示すスクリーンショットは次のとおりです。

enter image description here

26
jimbob

エラーメッセージが何を伝えているかを読んでください!

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

実装する必要があるメソッドは、tableView:numberOfRowsInSection:と呼ばれます。それは完全に異なっています。

32
matt

I had delegate and datasource hooked up to the TableView

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

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

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

  1. viewControllerを押して選択します
  2. 右側の接続インスペクターReferencing Outletsを探します
  3. 新しい参照アウトレットの円からビューにドラッグします
  4. マウスを離すと、小さなポップアップが表示されます-selectdatasource
  5. ステップ3を繰り返し、今回はdelegateを選択します

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

Referencing outlet Datasource and delegate connected to view

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

27
uplearnedu.com

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

Enter your custom view controller in identity inspector

11
DonVaughn

UITableViewDelegate、UITableViewDataSourceがクラスファイルで定義されているかどうかを確認します。

class ClassName: UIViewController, UITableViewDelegate, UITableViewDataSource  
{

}
10
ninahadi

それは非常に簡単です、私は同様の問題を抱えていました。 ViewControllerにStoryboardを使用せずにTableViewを実装しています。ViewControllerは、UIViewControllerを2つのプロトコルでサブクラス化しています:_<UITableViewDataSource, UITableViewDelegate>_

しかし、UITableViewDataSourceを見ると、_@required_という2つのメソッドがあり、これらをコードに実装する必要があります。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

UITableViewDelegateの他のすべてのメソッドは_@optional_であるため、それらを実装する必要はありません。

4
horkavlna

また、View Controllerですべてを適切に接続したにもかかわらず、UITableViewDelegate、UITableViewDataSourceを拡張するのを忘れた場合にも、このエラーが表示されます。

4
user10375102

この答えを示すことは重要だと思います。馬鹿げているようですが、おそらくそうですが、エラーを見るまでしばらく失いました。

新しいビューを作成し、コードの一部をコピーしました。

新しいビューを作成しました:

class THIRD: UIViewController {

[...]

}

そして、コードのコピーを開始しました。

func numberOfSections(in tableView: UITableView) -> Int {
...
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
...
}

等。

そして、メッセージエラー:

[Fonts.THIRD tableView:numberOfRowsInSection:]:インスタンス0x10204b200に送信された認識されないセレクター

ストーリーボードを何度も確認しました。そして、コードを何度もチェックしました。そして、私は楽しい時間を失いました。

ついに気づいた!!!

宣言するのを忘れました:

class THIRD: UIViewController, UITableViewDelegate, UITableViewDataSource {

おそらく、エラーはITableViewDelegateおよびITableViewDataSourceプロトコルを宣言しないでください。

忘れないでください。

2
Markus
- (NSInteger)numberOfRowsInSection:(NSInteger)section;

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

2
brynbodayle

すべてを正しくセットアップしても、このエラーが表示される場合。カスタムクラスにコントローラーを割り当てる必要があります enter image description here

0
Soufiane