xcode tableview 不工作 - [UIViewController tableView:numberOfRowsInSection:]: 无法识别的选择器发送到实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17134270/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:30:45  来源:igfitidea点击:

tableview not working - [UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance

iosobjective-cxcodeuitableviewxib

提问by Alessandro Garcez

I'm trying to init a uitableviewusing xibbut when I run the app in simulator, the below exception is thrown.

我正在尝试初始化uitableviewusingxib但是当我在模拟器中运行应用程序时,会引发以下异常。

2013-06-16 10:40:48.552 CoreDataExample[60661:c07] -[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x81765a0
2013-06-16 10:40:48.554 CoreDataExample[60661:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x81765a0'

Below the steps that i have followed trying to start the tableview:

在我尝试启动 tableview 所遵循的步骤下面:

  1. Add UITableViewDelegateand UITableViewDataSourcein my viewController.
  2. Insert the tableviewinto a view in my viewController.xib.
  3. Create datasourceand delegatein it files's owner (pressing control key and draging the mouse's arrow from component to file's owner and selecting the option delegate for example).
  4. Implement the methods - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionand - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathin my viewController.m.
  1. 添加UITableViewDelegateUITableViewDataSourceviewController
  2. 将 插入tableview到我的viewController.xib.
  3. 创建datasourcedelegate在其中创建文件的所有者(例如,按下控制键并将鼠标箭头从组件拖到文件的所有者并选择选项委托)。
  4. 实现方法- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath在我的viewController.m.

Below the implemetation of two methods:

下面是两种方法的实现:

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

    return 10;

}



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

    UITableViewCell *cell = nil;

    static NSString *identifier = @"identifier";

    cell = [tableView dequeueReusableCellWithIdentifier:identifier];

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

    cell.textLabel.text = @"Olá";
    cell.detailTextLabel.text = @"Subtitle" ;
    [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];


    return cell;
}

ViewController.h below:

ViewController.h 如下:

@interface CDEMainViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

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

@end

回答by IronMan

In XIB, Click on the File Owner, Check the Class Type. I guess it is UIViewController. If it is UIViewControllerchange it to CDEMainViewController. It will work properly.

在 XIB 中,单击文件所有者,检查类类型。我猜它是 UIViewController。如果是UIViewController将其更改为CDEMainViewController. 它会正常工作。

回答by Devendra Singh

In your ViewController XIB, Right click on the Tableview, Check the tableview datasource and delegate may be connected with view, not with File Owner, Disconnect these and connect again tableview datasource and delegate with File Owner, This resolved my issue. Please check screen shots also for refrence.

在您的 ViewController XIB 中,右键单击 Tableview,检查 tableview 数据源和委托可能与视图连接,而不是与文件所有者连接,断开这些并再次连接 tableview 数据源和委托与文件所有者,这解决了我的问题。请检查屏幕截图以供参考。

InCorrect Image

图像不正确

enter image description here

在此处输入图片说明

Correct Image

正确的图像

enter image description here

在此处输入图片说明

回答by edzio27

Have you set method?:

你设置方法了吗?:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

回答by stosha

Try open xib-file and reconnect IBOutlet of the tableView. Hope that helps you.

尝试打开 xib 文件并重新连接 tableView 的 IBOutlet。希望能帮到你。