简体   繁体   English

当两次选择同一行时,UITable崩溃

[英]UITable crashing when same row is selected twice

I have a UITableView, and when didSelectRowAtIndexPath is called on a row, it switches perfectly to the next view. 我有一个UITableView,并且在一行上调用didSelectRowAtIndexPath时,它会完美切换到下一个视图。 However, when I click the 'back' buttton and then select the same row previously selected...my app crashes. 但是,当我单击“后退”按钮,然后选择先前选择的同一行时...我的应用程序崩溃。 It does not crash if I select a different row. 如果选择其他行,它不会崩溃。 Here's the code: 这是代码:

- (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];
}

Crashes with “EXC_BAD_ACCESS” message. 使用“EXC_BAD_ACCESS”消息崩溃。

As you can see, I am printing the pointer address of the NSDictionary, and it seems to be looking for the same address for each individual indexPath.row. 如您所见,我正在打印NSDictionary的指针地址,似乎正在为每个单独的indexPath.row寻找相同的地址。 This means that the pointer location is being released, and when I try to reasign it to a value of the same indexPath.row, the old pointer address is being searched for, yet it does not exist. 这意味着正在释放指针位置,并且当我尝试将其重新分配给相同indexPath.row的值时,正在搜索旧的指针地址,但它不存在。 Maybe I'm totally wrong here. 也许我在这里完全错了。 Any help is appreciated. 任何帮助表示赞赏。

There's nothing obviously wrong with the code you've posted. 您发布的代码显然没有错。 My guess would be that you're overreleasing one of your data objects somewhere within CardPageViewController and then when your code tries to make a temporary dictionary from the same data again, it encounters a dealloced object within the dictionary and that's when the crash happens. 我的猜测是,您在CardPageViewController某个地方CardPageViewController一个数据对象,然后当您的代码再次尝试根据相同数据制作一个临时字典时,它在字典中遇到了一个已分配对象,这就是崩溃发生的时间。

I have a "hack" of an answer that keeps the object alive before the retain count goes all the way down with this elusive autorelease. 我有一个“破解”的答案,可以通过这种难以捉摸的自动释放功能,在保留计数完全下降之前使对象保持活动状态。 I initialize the dictionary, and then set it so the retain count goes to 2, and the hidden (to my eyes) autorelease doesn't crash the program. 我初始化了字典,然后对其进行了设置,以使保留计数变为2,并且隐藏的(对我而言)自动释放不会使程序崩溃。 Here is the code: 这是代码:

- (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];
}

I hope this helps someone who can't find this released dictionary. 希望这对找不到此已发布词典的人有所帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM