iOS UITableView在滚动时崩溃

我在这里遇到了一个奇怪的问题。 我正在开发一个iOS应用程序(特别是iPad),我在某个时候使用UITableView来显示一个列表。

现在,当我在视图的边界内滚动(不在第一个元素上面,而不是在最后一个元素下面)时,它可以正常工作。 然而,当我滚动的时候,它只是猛烈地崩溃,没有其他消息:

  • 当我向下滚动到最后一个元素时, EXC_BAD_ACCESS
  • 当我滚动比第一个更高时,带有回溯的SIGABRT

我看着谷歌,似乎我释放一些对象太多,但我无法弄清楚哪些。

我也尝试在乐器中运行应用程序,但每次运行应用程序时,乐器窗口都会冻结,迫使我用手杀死它…当然,我得不到任何结果…

这里有一些相关的代码:

/* Returns the cells of the table view */ -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Create a new cell view static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // Configure the cell... cell.textLabel.text = [newestModules objectAtIndex:indexPath.row]; cell.textLabel.textColor = [UIColor whiteColor]; cell.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"Background-Texture-Dark-Small.png"]]; cell.imageView.image = [UIImage imageNamed:@"Icon-Maths.png"]; UIView *v = [[[UIView alloc] initWithFrame:cell.frame] autorelease]; // Set view background color v.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background-Texture-Dark-Small.png"]]; // This view will be activated when the cell is selected cell.selectedBackgroundView = v; return cell; } 

编辑:UITableView加载和卸载方法:

 - (void)viewDidLoad { [super viewDidLoad]; // Transparent background self.tableView.backgroundView = nil; // Generate list of newest modules. Will later look for them on the internet, but for now we only add some test examples. newestModules = [[NSMutableArray alloc] initWithObjects:@"Test 1", @"Test 2", @"Test 3", @"Test 4", @"Test 5", nil]; } - (void)viewDidUnload { [newestModules release]; [super viewDidUnload]; } 

看起来,当你在界面生成器中添加一个对象,就像一个新的控制器,它默认是自动发布的。

如果您没有将其与类中的保留属性链接,则会在初始化后立即释放,导致可怕的EXC_BAD_ACCESS错误。

从经验来看,在界限和界限之间滚动的最大区别在于界限遍及表格的所有元素,包括页眉和页脚,并且很可能重新计算行数。 如果你有一个页眉和页脚,试着把断点放在那里。

关于EXC_BAD_ACCESS,您可以在malloc_error_break中放置一个断点,以更好地了解谁没有正确释放。 这是断点窗口上用“+”button定义的符号断点。