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

Ale*_*cez 9 xcode objective-c uitableview xib ios

我正在尝试初始化uitableview使用xib但是当我在模拟器中运行应用程序时,抛出以下异常.

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'
Run Code Online (Sandbox Code Playgroud)

在我尝试启动tableview之后的步骤下面:

  1. 添加UITableViewDelegate并UITableViewDataSource在我的viewController.
  2. 将其插入tableview我的视图中viewController.xib.
  3. 创建文件所有者datasource并delegate在其中(按下控制键并从组件到文件的所有者拖动鼠标箭头,然后选择选项委托).
  4. 实施方法- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section和- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath我的方法viewController.m.

下面是两种方法的实现:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

ViewController.h如下:

@interface CDEMainViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

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

@end
Run Code Online (Sandbox Code Playgroud)

Iro*_*Man 22

在XIB中,单击文件所有者,检查类类型.我想这是UIViewController.如果是UIViewController改变它CDEMainViewController.它会正常工作.


Dev*_*ngh 5

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

InCorrect Image

在此输入图像描述

正确的形象

在此输入图像描述