[英]tableView.datasource = self error:unrecognized selector
當我描述tableView.datasource = self
,
該應用程序異常終止並signal SIGABRT(unrecognized selector sent to instance)
。
當我刪除tableView.datasource = self
,
應用程序運行,但未反映數據源方法( cellForRowInSection
等)。
為了管理tableView
,我使用UIViewController
子類。
該視圖由多個子視圖組成,其中只有一個是tableView
。
ViewController.h
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
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;
}
錯誤信息 - - - - -
當-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’
當-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'
謝謝。
看到此示例代碼,我認為錯誤可能是_data1[section]
不是能夠使用選擇器count
。
當您刪除ligne _tableView.dataSource = self;
方法tableView:numberOfRowsInSection:
不會被調用,並且您的應用程序不會崩潰。
當將視圖控制器設置為表視圖的數據源時,視圖控制器必須遵循UITableViewDataSource協議。
該協議中有2種必需的方法:
– tableView:cellForRowAtIndexPath:必需的方法– tableView:numberOfRowsInSection:必需的方法
還有許多可選方法,包括
– numberOfSectionsInTableView:– sectionIndexTitlesForTableView:– tableView:sectionForSectionIndexTitle:atIndex:– tableView:titleForHeaderInSection:– tableView:titleForFooterInSection:
如果您至少沒有實現2種必需的方法,您的程序將崩潰。
按照其他人的要求,您需要復制並粘貼所收到的確切錯誤消息。 這將幫助我們了解問題所在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.