2012-09-14 13 views
15

Ho un problema strano. Ottengo questo errore:tableView: numberOfRowsInSection:]: selettore non riconosciuto inviato all'istanza

-[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 così ho fatto in modo che il ViewController è impostato come il DataSource e il Delegato. Ho aggiunto UITableViewDelegate e UITableViewDataSource alle cose <>.

Ho impostato tableView.delegate = self e tableView.dataSource = self nel mio metodo viewDidLoad.

Qui è la mia implementazione dei metodi viewDidLoad e 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]; 

} 

Ecco il mio corpo del metodo per numberOfRowsInSection:

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

Ecco il mio file di intestazione per il controller della vista:

#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 

Ecco uno screenshot che mostra come è collegato il mio TableView:

enter image description here

risposta

29

Leggi cosa ti dice il messaggio di errore!

Hai implementato numberOfRowsInSection:. E allora? Questo è il metodo sbagliato. È irrilevante. Non sarà mai chiamato.

Il metodo che è necessario implementare è chiamato tableView:numberOfRowsInSection:. Questo è completamente diverso.

+0

Posso avere qualche esempio di codice di questo metodo? Non riesco a trovarlo. – jimbob

+0

http://developer.apple.com/library/ios/documentation/userexperience/conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html#//apple_ref/doc/uid/TP40007451-CH6-SW5 – matt

+0

documenti: http: // developer .apple.com/library/ios/# documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html – matt

0
- (NSInteger)numberOfRowsInSection:(NSInteger)section; 

non si sta implementando il metodo UITableViewDataSource. Rimuovi la dichiarazione e non si bloccherà più. In alternativa, fornisci un metodo per il suo corpo.

+0

ho un corpo del metodo per questo: – jimbob

+0

- (NSInteger) numberOfRowsInSection: sezione (NSInteger) { return 5; } – jimbob

+1

Ah, beh, sarebbe stato utile inserire la domanda originale prima di modificarla;) – brynbodayle

4

È molto semplice, ho avuto un problema simile. Io sono l'attuazione mia TableView senza Storyboard nel mio ViewController che ha sottoclasse UIViewController con due protocolli: <UITableViewDataSource, UITableViewDelegate>

Ma se si guarda al UITableViewDataSource ci sono due metodi strega sono @required e questi si devono implementare nel codice:

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

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

Tutti gli altri metodi in UITableViewDelegate sono @optional quindi non occorre per la loro attuazione.

24

I had delegate and datasource hooked up to the TableView

Se si fa clic sulla vista dovrebbe mostrare dei collegamenti in ISPETTORE. Se non hai il ViewController collegato così si otterrà l'errore:

'NSInvalidArgumentException', reason: '-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance

Come mi sono fermato l'errore si è

  1. premere sul ViewController per selezionarlo
  2. sguardo per Punti di riferimento nella Ispettore di connessione a destra
  3. trascinamento dal cerchio dello Ne w Riferimento uscita alla vista
  4. quando si rilascia il mouse, un piccolo popup mostrerà - selezionare origine dati
  5. ripetere il punto 3, e questa volta selezionare delegato

Al termine, si dovrebbe assomigliare all'immagine qui sotto!

Referencing outlet Datasource and delegate connected to view

Questa fermato l'errore precedente per me. Spero che questo aiuti qualcuno.

+0

Questa ottima risposta mostra come risolverlo tramite ** Connections Inspector **, che equivale a ciò che @matt realizza via codice – Gonen

9

Si vedrà anche questo errore se, anche se aver collegato tutto correttamente nel controller della vista, si è dimenticato di assegnare la visualizzazione classe controller personalizzato nel storyboard:

Enter your custom view controller in identity inspector

+0

Questo era il problema nel mio caso –

Problemi correlati