【问题标题】:UITable crashing when same row is selected twice两次选择同一行时UITable崩溃
【发布时间】:2011-01-26 22:10:04
【问题描述】:

我有一个 UITableView,当在一行上调用 didSelectRowAtIndexPath 时,它完美地切换到下一个视图。但是,当我单击“后退”按钮,然后选择之前选择的同一行时……我的应用程序崩溃了。如果我选择不同的行,它不会崩溃。代码如下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSDictionary *tempEventDictionary = [[NSDictionary alloc]initWithDictionary:[arrayWithEvents objectAtIndex:indexPath.row]];
    NSLog(@"%i",tempEventDictionary);


    //push to new view and set myArray in the cardPage
    CardPageViewController *cardPageViewController = [[CardPageViewController alloc] init];
    cardPageViewController.eventDictionary = tempEventDictionary;
    [self presentModalViewController:cardPageViewController animated:YES];

    [cardPageViewController release];   
    [tempEventDictionary release];
}

“EXC_BAD_ACCESS” 消息崩溃。

如您所见,我正在打印 NSDictionary 的指针地址,它似乎正在为每个单独的 indexPath.row 寻找相同的地址。这意味着指针位置正在被释放,当我尝试将其重新分配为相同 indexPath.row 的值时,正在搜索旧的指针地址,但它不存在。也许我在这里完全错了。任何帮助表示赞赏。

【问题讨论】:

  • 崩溃的输出是什么?
  • 没有输出。只是一个崩溃。我想有一次我试图让我的字典自动释放时看到了 malloc 的东西。
  • 当然有一些输出。检查 gdb 控制台并粘贴最后一条消息。另外,为什么要使用 int 格式说明符 ("%i") 打印 NSDictionary?
  • 我正在使用 int 格式打印以查看指针位置。我的控制台中没有崩溃的输出,只有从我的 NSLogs 打印的值。
  • 抱歉,收到“EXC_BAD_ACCESS”消息。

标签: iphone memory uitableview


【解决方案1】:

您发布的代码没有明显的问题。我的猜测是你在CardPageViewController 的某个地方过度释放了一个数据对象,然后当你的代码尝试再次从相同的数据创建一个临时字典时,它会在字典中遇到一个释放的对象,那就是崩溃发生的时候.

【讨论】:

    【解决方案2】:

    我有一个答案的“破解”,它可以在保留计数因这种难以捉摸的自动释放而一直下降之前使对象保持活动状态。我初始化字典,然后将其设置为保留计数为 2,并且隐藏的(在我看来)自动释放不会使程序崩溃。代码如下:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //---send data to cardPage
    
    CardPageViewController *cardPageViewController = [CardPageViewController alloc];
    
    NSDictionary *tempEventDictionary = [[NSDictionary alloc]initWithDictionary:[arrayWithEvents objectAtIndex:indexPath.row]];
    cardPageViewController.eventDictionary = [arrayWithEvents objectAtIndex:indexPath.row];
    
    [self presentModalViewController:cardPageViewController animated:YES];
    [cardPageViewController release];
    }
    

    我希望这对找不到这本已发布词典的人有所帮助。

    【讨论】:

    • 永远不要alloc 没有立即init'ing。
    【解决方案3】:

    为什么不创建一个接受字典的自定义 init,然后将字典复制到新视图控制器的自定义 init 中?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-29
      • 1970-01-01
      相关资源
      最近更新 更多