2012-09-14 22 views
15

Tôi gặp vấn đề lạ. Tôi gặp lỗi này:tableView: numberOfRowsInSection:]: bộ chọn không được nhận dạng được gửi đến dụ

-[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 vì vậy tôi đã đảm bảo rằng ViewController được đặt làm dataSource và Ủy quyền. Tôi đã thêm UITableViewDelegate và UITableViewDataSource vào các điều khoản <>.

Tôi đã đặt tableView.delegate = self và tableView.dataSource = self trong phương thức viewDidLoad của tôi.

Đây là triển khai thực hiện của tôi về phương pháp viewDidLoad và 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]; 

} 

Đây là cơ thể phương pháp của tôi cho numberOfRowsInSection:

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

Đây là tập tin header của tôi cho bộ điều khiển xem:

#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 

Dưới đây là ảnh chụp màn hình hiển thị cách xem bảng của tôi được ghép nối:

enter image description here

Trả lời

29

Đọc thông báo lỗi cho bạn biết điều gì!

Bạn đã triển khai numberOfRowsInSection:. Vậy cái gì? Đó là phương pháp sai. Nó không liên quan. Nó sẽ không bao giờ được gọi.

Phương pháp bạn cần triển khai được gọi là tableView:numberOfRowsInSection:. Điều đó hoàn toàn khác.

+0

Tôi có thể có một số ví dụ mã của phương pháp này không? Tôi không thể tìm thấy nó. – 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

tài liệu: http: // nhà phát triển .apple.com/library/ios/# documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html – matt

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

Bạn không thực hiện phương pháp UITableViewDataSource. Loại bỏ tuyên bố và nó sẽ không sụp đổ nữa. Hoặc cách khác cung cấp một phương thức cho nó.

+0

tôi có một phương thức cho nó: – jimbob

+0

- (NSInteger) numberOfRowsInSection: (NSInteger) phần { return 5; } – jimbob

+1

Ahh, điều đó cũng hữu ích khi đặt câu hỏi gốc trước khi bạn chỉnh sửa nó;) – brynbodayle

4

Nó rất đơn giản, tôi đã có một vấn đề tương tự. Tôi đang thực hiện TableView của tôi mà không Storyboard trong ViewController tôi đã subclassed UIViewController với hai giao thức: <UITableViewDataSource, UITableViewDelegate>

Nhưng nếu bạn nhìn vào UITableViewDataSource có hai phương pháp phù thủy là @required và các bạn cần phải thực hiện trong mã của bạn:

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

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

Tất cả các phương pháp khác trong UITableViewDelegate@optional do đó bạn không cần phải thực hiện chúng.

24

I had delegate and datasource hooked up to the TableView

Nếu bạn click vào xem nó sẽ hiển thị các kết nối trên trong THANH TRA. Nếu bạn không có các ViewController nối cũng bạn sẽ nhận được lỗi:

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

Làm thế nào tôi dừng lại lỗi là bạn

  1. báo chí trên ViewController để chọn nó
  2. nhìn cho Tham chiếu Cửa hàng trong thanh tra Kết nối ở bên phải
  3. kéo từ vòng tròn của Ne w Tham khảo Outlet đến xem
  4. khi bạn thả chuột, một nhỏ cửa sổ bật lên sẽ hiển thị - chọn nguồn dữ liệu
  5. lặp lại bước 3, và lần này chọn đại biểu

Khi hoàn tất, nó sẽ giống như hình dưới đây!

Referencing outlet Datasource and delegate connected to view

này ngừng các lỗi trên cho tôi. Tôi hy vọng điều này sẽ giúp ai đó.

+0

Câu trả lời tuyệt vời này cho thấy cách giải quyết nó thông qua ** Trình kiểm tra kết nối **, tương đương với những gì @matt hoàn thành thông qua mã. – Gonen

9

Bạn cũng sẽ thấy lỗi này nếu, mặc dù bạn đã nối tất cả mọi thứ lên đúng cách trong bộ điều khiển xem, bạn đã quên gán tùy chỉnh xem lớp điều khiển của bạn trong kịch bản của bạn:

Enter your custom view controller in identity inspector

+0

Đó là vấn đề trong trường hợp của tôi –

Các vấn đề liên quan