簡體   English   中英

-[UIViewController tableView:numberOfRowsInSection:]:無法識別的選擇器已發送到實例0x7fc748e37ea0'

[英]-[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fc748e37ea0'

我正在忙着制作一個從.plist加載大量名稱的應用程序。 我已經進行了設置,以使.plist加載到UiTableView中。 現在的問題是,每當嘗試打開帶有名稱表的菜單時,都會收到錯誤代碼。

這是錯誤消息:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fc748e37ea0'

這是我的viewController.m

#import "ViewController.h"
#import "Stuff.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

Stuff *s = [[Stuff alloc] init];

[s getStuff];

self.items = [[NSMutableArray alloc] initWithArray:s.stuff];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.items count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath*)indexPath
{
NSString *id = @"plistdata";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:id forIndexPath:indexPath];

if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:id];
}

cell.textLabel.text = self.items[indexPath.row];

return cell;
}


@end

謝謝!

根據Matt所說的:您得到的錯誤是UIViewController類(所有視圖控制器的基類)無法識別消息tableView:numberOfRowsInSection:

以某種方式在創建視圖控制器時,將創建通用的UIViewController對象,而不是自定義ViewController類的實例。

其原因取決於您如何創建視圖控制器。

如果使用調用-[UIViewController initWithNibName:bundle:]代碼來創建它,則可能是在創建UIViewController的實例,而不是自定義的ViewController類:

ViewController *myVC = [[UIViewController alloc] initWithNibName: @ViewController" 
  bundle: nil];

如果使用情節提要創建它,則可能是情節提要配置不正確。

為了幫助您弄清楚為什么要獲得通用的UIViewController而不是自定義類,您需要告訴我們如何創建自定義視圖控制器的實例。

問題是您顯示的代碼永遠不會被調用,因為您顯示的視圖控制器(ViewController)不是表視圖的數據源/委托。 數據源/委托消息被發送到其他視圖控制器。 (這可能是因為情節提要板配置錯誤,但是您沒有提供足夠的信息讓我確定這一點。)

驗證接口構建器中的“文件的所有者”對象是否設置為要實現的UITableViewController子類的實例。 還要確保tableView出口已鏈接到您的表視圖。

.h文件

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

故事板設置

屬性檢查器:

在此處輸入圖片說明

連接檢查器:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM