简体   繁体   English

tableView.datasource =自身错误:无法识别的选择器

[英]tableView.datasource = self error:unrecognized selector

When I describe tableView.datasource = self , 当我描述tableView.datasource = self
the app abend with signal SIGABRT(unrecognized selector sent to instance) . 该应用程序异常终止并signal SIGABRT(unrecognized selector sent to instance)

When I erase tableView.datasource = self , 当我删除tableView.datasource = self
the app run but datasource method( cellForRowInSection etc) is not be reflected. 应用程序运行,但未反映数据源方法( cellForRowInSection等)。

To manage the tableView , I use a UIViewController subclass. 为了管理tableView ,我使用UIViewController子类。
The view is composed of multiple subviews, only one of which is a tableView . 该视图由多个子视图组成,其中只有一个是tableView

ViewController.h ViewController.h

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

ViewController.m ViewController.m

    @interface ViewController ()
    @property (retain, nonatomic) UITableView *tableView;
    @end
    @implementation ViewController{
      @private NSArray *_data1;
    @synthesize tableView = _tableView;

    - (void)viewDidLoad
    {
      _tableView = [[UITableView alloc]initWithFrame:CGRectMake(-10, 70, 320, 480)];
      _tableView.dataSource = self;
      _tableView.delegate = self;
      [self.view addSubview:_tableView];
    }

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
      return [_data1 count];
    }

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

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      static NSString *CellIdentifier = @"Cell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
      if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
      }
      NSString *data;
      data = _data1[indexPath.section][indexPath.row];
      cell.textLabel.text = data;
      return cell;
    }

ERROR MESSAGE--------- 错误信息 - - - - -

when -tableView:numberOfRowsInSection return 1; -tableView:numberOfRowsInSection return 1;

Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2903.23/UITableView.m:5261

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard’

when -tableView:numberOfRowsInSection return [_data1[section]count] -tableView:numberOfRowsInSection return [_data1[section]count]

[__NSCFConstantString count]: unrecognized selector sent to instance 0x100006930
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString count]: unrecognized selector sent to instance 0x100006930'

Thank you. 谢谢。

Seeing this sample code, I think that the error can be that _data1[section] is not an object that is able to use the selector count . 看到此示例代码,我认为错误可能是_data1[section]不是能够使用选择器count

When you remove the ligne _tableView.dataSource = self; 当您删除ligne _tableView.dataSource = self; the method tableView:numberOfRowsInSection: is not called and your app does not crash. 方法tableView:numberOfRowsInSection:不会被调用,并且您的应用程序不会崩溃。

When you set your view controller as the datasource for the table view, your view controller must then conform to the UITableViewDataSource protocol. 当将视图控制器设置为表视图的数据源时,视图控制器必须遵循UITableViewDataSource协议。

There are 2 required methods in that protocol: 该协议中有2种必需的方法:

– tableView:cellForRowAtIndexPath: required method – tableView:numberOfRowsInSection: required method – tableView:cellForRowAtIndexPath:必需的方法– tableView:numberOfRowsInSection:必需的方法

There are also a number of optional methods, including 还有许多可选方法,包括

– numberOfSectionsInTableView: – sectionIndexTitlesForTableView: – tableView:sectionForSectionIndexTitle:atIndex: – tableView:titleForHeaderInSection: – tableView:titleForFooterInSection: – numberOfSectionsInTableView:– sectionIndexTitlesForTableView:– tableView:sectionForSectionIndexTitle:atIndex:– tableView:titleForHeaderInSection:– tableView:titleForFooterInSection:

If you don't at least implement the 2 required methods, your program will crash. 如果您至少没有实现2种必需的方法,您的程序将崩溃。

As the others have requested, you need to copy and paste the exact error message you are getting. 按照其他人的要求,您需要复制并粘贴所收到的确切错误消息。 That will help us understand what's going wrong. 这将帮助我们了解问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UITableView在tableview.datasource开始时崩溃 - UITableView crashes at start at tableview.datasource tableView.cellForRow(at :)和tableView.dataSource?tableView(tableView:cellForRowAt :)之间的区别 - Difference between tableView.cellForRow(at:) and tableView.dataSource?tableView(tableView:cellForRowAt:) tableview-无法识别的选择器didSelectRowAtIndexPath - tableview - unrecognized selector didSelectRowAtIndexPath [NSArrayM tableView]:无法识别的选择器 - NSArrayM tableView]: unrecognized selector 删除行时TableView“无法识别的选择器发送到实例”错误 - TableView “unrecognized selector sent to instance” error when deleting row 故事板上的2个表格视图的cellForRowAtIndexPath,无法识别的选择器发送到实例错误 - cellForRowAtIndexPath for 2 tableview in a storyboard, unrecognized selector sent to instance error 具有自定义tableview单元格的无法识别的选择器 - unrecognized selector with custom tableview cell -[__ NSDictionaryM isEqualToString:]:当我调用此方法时,无法识别的选择器发送到实例[self.tableView endUpdates] - -[__NSDictionaryM isEqualToString:]: unrecognized selector sent to instance ,when i call this method [self.tableView endUpdates] 收到无法识别的选择器错误 - Receiving an unrecognized selector error Xcode 无法识别的选择器错误 - Xcode Unrecognized Selector Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM