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. 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 re开发者_C百科lease];
[tempEventDictionary release];
}
Crashes with “EXC_BAD_ACCESS”
message.
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. 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. 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.
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. 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.
Why not create a custom init that takes a dictionary and then copy the dictionary inside the your custom init for the new view controller?
精彩评论