2010-04-29 74 views
5

我的UITableView返回EXEC_BAD_ACCESS,但爲什麼!EXEC_BAD_ACCESS在UITableView cellForRowAtIndexPath

查看此代碼段!

加載UITableView的工作正常,所以allXYZArray != nil和填充!

然後滾動的tableview的底部和備份導致其崩潰,因爲它會重新加載方法的cellForRowAtIndexPath

它未能就行:

"NSLog(@"allXYZArray::count: %i", [allXYZArray count]);" 

     (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAt 

IndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"CellIdentifier"; 
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
@try 
{ 
if (allXYZArray == nil) { 
    NSLog(@"nil"); 
    allXYZArray = [ToolBox getMergedSortedDictionaries:allXYZGiven SecondDictionary:allXYZSought]; 
} 
NSLog(@"%i", [indexPath row]); 
NSLog(@"allXYZArray::count: %i", [allXYZArray count]); 
+0

您可以將方法的其餘部分的代碼發佈[ToolBox getMergedSortedDictionaries:SecondDictionary:]的代碼? – MrHen 2010-05-12 19:16:13

回答

10

EXC_BAD_ACCESS表示您的程序嘗試訪問無效或無法從您的進程訪問的內存地址。當您嘗試將消息發送給已被處理的對象時,通常會發生這種情況。因此,調試EXC_BAD_ACCESS的第一步是找出程序在發生崩潰時試圖發送消息的對象。通常情況下,答案並不明顯,在這種情況下,NSZombieEnabled是識別哪一行代碼導致崩潰的很好工具。

在你的情況下,你已經確定當你致電[allXYZArray count]時,崩潰發生,使allXYZArray我們的主要嫌犯。這個對象從+[ToolBox getMergedSortedDictionaries:SecondDictionary:]返回,所以很可能你的錯誤在於這個方法的實現。我猜想它會返回一個已經被釋放而不是自動釋放的對象,如Memory Management Programming Guide for Cocoa所規定的。 (順便提一下,這是SDK中最重要的文檔之一,我建議每月重新閱讀一次,直到其政策和技術成爲第二性質。)

1

好,重用細胞,不不能保證該單元將被正確初始化:

UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

細胞有時爲空(特別是第一次我猜)。

檢查單元格是否爲空,如果是,請正確初始化它。

if (cell == nil) 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
相關問題