2010-04-29 79 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]; 
相关问题