2012-11-06 60 views
7

我有一個程序,其中有一個tableViewController有1節和3行。每當我運行應用程序,它總是在dequeueReusableCellWithIdentifier:方法崩潰。我在這個方法中插入了一個斷點並且將NSLog()歸零。我還將故事板中原型單元的Cell Identifier設置爲Cell。但該計劃仍然崩潰。下面是導致該問題的方法:程序在dequeueReusableCellWithIdentifier崩潰:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"indexPath.section = %d, indexPath.row = %d", indexPath.section, indexPath.row); 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    if (cell == nil) 
    { 
     NSLog(@"in nil"); 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 

    return cell; 
} 

崩潰日誌上寫着:

MyAppTake4[7463:707] indexPath.section = 0, indexPath.row = 0 
2012-11-05 23:21:20.747 MyCardAppTake4[7463:707] -[UITableView  dequeueReusableCellWithIdentifier:forIndexPath:]: unrecognized selector sent to instance  0xfbc9000 
2012-11-05 23:21:20.753 MyCardAppTake4[7463:707] *** Terminating app due to uncaught  exception 'NSInvalidArgumentException', reason: '-[UITableView  dequeueReusableCellWithIdentifier:forIndexPath:]: unrecognized selector sent to instance  0xfbc9000' 
*** First throw call stack: 
(0x3254788f 0x3459d259 0x3254aa9b 0x32549915 0x324a4650 0x7e2f7 0x31fceefb 0x31fcdfd9  0x31fcd763 0x31f71f15 0x324a61fb 0x3420aaa5 0x3420a6bd 0x3420e843 0x3420e57f 0x342064b9  0x3251bb1b 0x32519d57 0x3251a0b1 0x3249d4a5 0x3249d36d 0x315f4439 0x31f9ccd5 0x7d9f9 0x7d994) 
terminate called throwing an exception(lldb) 

我感興趣的識別錯誤的確切原因,這樣我就不會再犯這樣的錯誤。

感謝您的幫助。

+1

在iOS 6.0中添加了'UITableView'方法'dequeueReusableCellWithIdentifier:forIndexPath:'。你是否在iOS 5.x下得到這個錯誤? – rmaddy

回答

9

那是因爲[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];在IOS 6的溶液中加入,但iOS不5.識別對於iOS 5兼容性使用下面方法來代替:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
+5

'dequeueReusableCellWithIdentifier:forIndexPath:'是截至iOS 6.0的完全有效的方法。 – rmaddy

+3

感謝cyberpawn ..它的工作。只要刪除'forIndexPath:indexPath',程序就開始工作。當我創建一個帶有「UITableViewController」子類的新文件時,這個方法默認是存在的。如果'forIndexPath:indexPath'是罪魁禍首,爲什麼它默認出現在程序中? –

+0

由於您的評論幫助我首先確定問題,我接受了您的答案。我也非常感謝其他的參加! :-) –

7

你註冊類細胞或筆尖第一?

從Apple文檔:

重要:調用此方法之前的方法:你必須註冊使用registerNib類或筆尖文件:forCellReuseIdentifier:或的registerClass:forCellReuseIdentifier。

(順便說一句那個方法僅在iOS 6中使用)

如果你使用的是iOS 6,那麼你可以使用新的簡單的管理模式:

在viewDidLoad中,登記類,你的情況,這僅僅是UITableViewCell類,:

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; 

然後,你可以做這樣的的cellForRowAtIndexPath:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    cell.textLabel.text = self.myArray[indexPath.row]; 
    return result; 
} 
+0

我已經註冊了viewController的類。因爲我正在使用故事板,所以沒有'nib'。 「cyberpawn」幫助我找出錯誤。謝謝你的幫助。 –

+0

很棒..我會記住rdelmar! –

+0

在我的'viewDidLoad'中添加'registerClass'方法爲我做了竅門。謝謝。 –

1

由於forIndexPath方法適用於ios6,我建議您使用snipet。在下面找到。

UITableViewCell *cell = [table_view dequeueReusableCellWithIdentifier:@"cell"]; 
if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"]; 

    // **initialize** your stuff here **with their respected tag** 
} 
else 
{ 
    // **get** your stuff back using their tags 
} 
// **assign** your valiues here 

return cell; 

喜歡編程

+0

感謝Foram Mukund Shah! –

0

我有這個異常,並且意識到這是我愚蠢的錯誤造成的。我創建了一個空白的.xib文件(在我的UITableView中有一個自定義的「加載」單元格),但是之後拖放了一個View。衛生署!

的結果是,你所看到的異常信息,加上這一在輸出窗口:

2014-04-25 20:45:12.953 Languages[36256:60b] *** Terminating app due to uncaught 
exception 'NSInternalInconsistencyException', reason: 'invalid nib registered 
for identifier (LoadingCell) - nib must contain exactly one top level object 
which must be a UITableViewCell instance' 

的解決方案,當然,是一個Table View Cell添加到我的.xib文件,複製其他UI組件添加到其中,並將該表視圖單元保留爲文件中的「根」元素。

+0

是的,我不小心做了同樣的事情。我的.xib文件非常完美...但是我錯誤地將一個標籤拖放到了.xib之外,主視圖(嘆氣)粉碎另一個浪費時間。 –

0

此錯誤的另一個原因可以是─

爲標識符(小區)註冊無效筆尖 - 筆尖必須包含其必須爲一個UICollectionReusableView實例

我在XIB有一個UIView恰好一個頂層對象而不是一個collectionViewCell。希望這可以幫助。