2014-02-26 43 views
0

當我描述tableView.datasource = self
應用程序異常結束與signal SIGABRT(unrecognized selector sent to instance)

當我擦除tableView.datasource = self
該應用運行但數據源的方法(cellForRowInSection等)不被反射。tableView.datasource =自錯誤:無法識別選擇

要管理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' 

謝謝。

+0

用完整的錯誤更新您的問題。 – rmaddy

+1

你能寫出什麼是無法識別的選擇器? 是'count'?我敢打賭,它是一個友善的夥伴 – KIDdAe

+0

僅供參考 - 擺脫對「@綜合」的呼籲 - 這是不需要的。同時擺脫對「@私人」的呼叫。 '@ implementation'塊中的任何ivars都是私有的。 – rmaddy

回答

2

看到這個示例代碼,我認爲錯誤可能是_data1[section]不是能夠使用選擇器count的對象。

當您刪除ligne _tableView.dataSource = self;時,方法tableView:numberOfRowsInSection:未被調用,您的應用程序不會崩潰。

+0

確切地說,當'numberOfRowInSection {return 0;}',應用程序不會崩潰。 –

+0

@TeruyaKusumoto然後通過查看你的'tableView:CellForRowAtIndexPath:'看看你在_data1中添加了什麼,它應該是一個字符串數組的數組,但它似乎只是一個字符串數組。 對於使用'return 1'時出現的崩潰,嘗試使用'[tableView dequeueReusableCellWithIdentifier:CellIdentifier]'而不是'[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]' – KIDdAe

+0

問題解決了。謝謝。 –

0

當您將視圖控制器設置爲表視圖的數據源時,您的視圖控制器必須符合UITableViewDataSource協議。

有在方案2種所需的方法:

- 的tableView:的cellForRowAtIndexPath:所需的方法 - 的tableView:numberOfRowsInSection:所需的方法

也有許多可選的方法,包括

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

如果您沒有至少實現2個必需的方法,您的程序將崩潰。

正如其他人所要求的,您需要複製並粘貼您所得到的確切錯誤消息。這將幫助我們瞭解發生了什麼問題。

+0

謝謝你的回答。我粘貼錯誤信息。你能給我進一步的答案嗎? –

+0

問題解決了,謝謝。 –

+0

然後請接受答案,並在您有足夠的聲譽時再投票。 –