Xcode TableView - 无法识别的选择器发送到实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11093352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode TableView - unrecognized selector sent to instance
提问by Alex
I'm trying to learn table views and I've hit a snag. I have the view delegate and datasource connected correctly in Storyboard, but I get the following runtime error when I get to the section of my app containing the table view.
我正在尝试学习表格视图,但遇到了障碍。我在 Storyboard 中正确连接了视图委托和数据源,但是当我到达包含表视图的应用程序部分时出现以下运行时错误。
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationItem tableView:numberOfRowsInSection:]: unrecognized selector sent to instance
Here's the chunk from my implementation file
这是我的实现文件中的块
@implementation CraftingViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = @"Detail";
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
回答by Jason Kulatunga
That error message is displaying because tableView:numberOfRowsInSection:
is being sent to an object of type UINavigationItem
while it seems like your CraftingViewController
class is probably of type UITableViewController
. I would make sure that you have connected your delegate to the correct class, because it doesn't seem like CraftingViewController
is connected properly.
显示该错误消息是因为tableView:numberOfRowsInSection:
它被发送到一个类型的对象,UINavigationItem
而您的CraftingViewController
类可能是类型的UITableViewController
。我会确保您已将您的委托连接到正确的类,因为它似乎没有CraftingViewController
正确连接。
回答by Siemen
if the datasource is controller of the nib file.
如果数据源是 nib 文件的控制器。
please check alloc of this controller use "CraftingViewController" not "UIViewController"
请检查此控制器的分配使用“CraftingViewController”而不是“UIViewController”