xcode 问题将 tableView 连接到 .XIB 中的数据源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18255231/
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
Issue Connecting tableView to Data Source in .XIB
提问by ScottOBot
So I'm trying to connect a tableView component to a UIViewController created through the .xib user interface. I am getting the classic -[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x1f544cb0
and am guessing that I have connected something incorrectly or am doing something that I shouldn't be doing with the interface builder. I have searched google and stack overflow and have found similar questions but non with answers that have helped my situation. I have been hitting my head trying to figure this out and hope someone here can help. I have only ever done this through storyboard or with .xib when the controller was a UITableViewController. Usually I would just use a UITableViewController but I may need to add a toolbar on the bottom of the view so that option is not viable. Here are the steps I took to create the class please let me know if I am doing something wrong
所以我试图将 tableView 组件连接到通过 .xib 用户界面创建的 UIViewController。我得到了经典-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x1f544cb0
,我猜我连接不正确,或者正在做一些我不应该用界面构建器做的事情。我搜索过谷歌和堆栈溢出,发现了类似的问题,但没有找到对我的情况有帮助的答案。我一直在努力解决这个问题,希望这里有人可以提供帮助。当控制器是 UITableViewController 时,我只通过 Storyboard 或 .xib 完成了此操作。通常我只会使用 UITableViewController 但我可能需要在视图底部添加一个工具栏,以便该选项不可行。以下是我创建课程所采取的步骤,如果我做错了什么,请告诉我
I first create the file with an XIB for user interface:
我首先使用用户界面的 XIB 创建文件:
I then add the table view component to the XIB:
然后我将表视图组件添加到 XIB:
In the LeftMenuViewController.h I add the following line of code to tell the allow for the class to be used as a UITableViewDelegate and UITableViewDataSource:
在 LeftMenuViewController.h 中,我添加了以下代码行以告知允许将该类用作 UITableViewDelegate 和 UITableViewDataSource:
@interface LeftMenuViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@interface LeftMenuViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
I then connect the table view component up to the delegate and data source by control dragging from the component to the File's Owner heading and selecting the delegate and data source as can be seen:
然后,我通过控制从组件拖动到 File's Owner 标题并选择委托和数据源,将表视图组件连接到委托和数据源,如下所示:
I then add the following lines of code to LeftMenuViewController.m to provide definitions for the required TableView delegate methods:
然后,我将以下代码行添加到 LeftMenuViewController.m 以提供所需 TableView 委托方法的定义:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 48;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
return cell;
}
This process causes the app to throw a NSInvalidArgumentException which produces the [UIViewController tableView:numberOfRowsInSection:]: unrecognized selector
error. What am I doing wrong? Everything I have tried or have been recommended hasn't worked.
此过程会导致应用程序抛出产生[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector
错误的 NSInvalidArgumentException 。我究竟做错了什么?我尝试过或被推荐的一切都没有奏效。
采纳答案by Rob
The fact that the error is referencing UIViewController
in your error message, [UIViewController tableView:numberOfRowsInSection:]: unrecognized selector
, that means that the file owner has not been set (it should be referencing your view controller subclass, not UIViewController
, itself). You've correctly specified the delegate and data source in your NIB, so I wonder how you instantiated your view controller.
错误UIViewController
在您的错误消息中引用的事实[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector
,这意味着尚未设置文件所有者(它应该引用您的视图控制器子类,而不是UIViewController
本身)。您已经在 NIB 中正确指定了委托和数据源,所以我想知道您是如何实例化视图控制器的。
You should have instantiated it with something like:
您应该使用以下内容实例化它:
SecondViewController *controller = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:controller animated:YES completion:nil];
Or, if the NIB name doesn't match the name of the controller:
或者,如果 NIB 名称与控制器名称不匹配:
SecondViewController *controller = [[SecondViewController alloc] initWithNibName:@"MyNibName" bundle:nil];
[self presentViewController:controller animated:YES completion:nil];
回答by Chris Tetreault
Click on File's Owner in your NIB file in the Placeholders section and make sure the custom class is set to LeftMenuViewController
or whatever you named it.
在占位符部分的 NIB 文件中单击文件的所有者,并确保自定义类设置为LeftMenuViewController
或您命名的任何内容。
回答by Abdullah Shafique
In viewDidLoad:
在 viewDidLoad 中:
self.tableView.delegate = self;