【问题标题】:iOS UITableView crashes when scrollingiOS UITableView 滚动时崩溃
【发布时间】:2011-10-05 23:53:25
【问题描述】:

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

现在,当我在视图边界内滚动时(不在第一个元素之上,也不在最后一个元素之下),它可以正常工作。但是,当我滚动得更远时,它只会剧烈崩溃,除了以下消息之外没有其他消息:

  • EXC_BAD_ACCESS 当我向下滚动到最后一个元素时
  • SIGABRT 当我滚动到第一个上方时带有回溯

我在 Google 上查看过,似乎我释放了一些对象太多,但我不知道是哪些对象。

我也尝试在 Instruments 中运行该应用程序,但每次我运行该应用程序时 Instruments 窗口都会冻结,迫使我手动将其杀死...当然我没有得到任何结果...

这里有一点相关代码:

    /*
     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];
}

【问题讨论】:

  • 当您注释掉return cell(这个 selectedNackgroundView 的东西)之前的最后 3 行相关行时,是否仍然出现崩溃?
  • 即使我注释掉“配置单元格”和“返回单元格”之间的每一行,它实际上也会发生......这就是为什么我有点失落......
  • 您的最新模块是如何定义(包括属性)、分配和填充的?
  • 我用 viewDidLoad 和 viewDidUnload 方法编辑了我的帖子,因为我在这里分配东西。
  • 在获得 EXC_BAD_ACCESS 后您应该尝试的第一件事是运行产品 > 分析 (⇧⌘B) 并设置 NSZombieEnabled。总是这样做。大多数时候它会直接告诉你哪里出错了。

标签: ios uitableview scroll exc-bad-access


【解决方案1】:

似乎,当您在 Interface Builder 中添加一个对象(如新控制器)时,默认情况下它是自动释放的

如果您不将它与类中的 retained 属性链接,它会在初始化后立即释放,从而导致可怕的 EXC_BAD_ACCESS 错误。

【讨论】:

    【解决方案2】:

    根据经验,在边界内滚动和在边界滚动之间的最大区别在于,在边界滚动它会遍历表格的所有元素,包括页眉和页脚,并且很可能会重新计算行数。如果您有页眉和页脚,请尝试在此处放置断点。

    关于 EXC_BAD_ACCESS,您可以在 malloc_error_break 中放置一个断点,以希望更多地了解哪些人没有正确释放。这是一个符号断点,用断点窗口上的“+”按钮定义。

    【讨论】:

    • 我实际上没有任何页眉或页脚(我删除了 numberOfSectionsInTableView 方法)...我将尝试设置断点,看看会发生什么!
    • 我刚刚尝试设置断点,但是没有触发,同样的EXC_BAD_ACCESS发生了。
    猜你喜欢
    • 2011-07-10
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多