开发者

didSelectRowAtIndexPath generates EXC_BAD_ACCESS while willSelectRowAtIndexPath works fine UITableView

I'm starting out with iPhone development, and have a bit of a problem with programming a UITableView which is a subpart of a regular UIViewController.

The Problem:

When tapping a row, the didSelectRowAtIndexPath generates an EXC_BAD_ACCESS (I've checked for the correct method signature. Whereas if I move the body of this method to willSelectRowAtIndexPath it works fine. This seems wrong to me as all the documentation/patterns use didSelectRowAtIndexPath. Ok, so whats really weird is whether the method is called at all. Surely if the following log output is not logged, then the method did not run, so I could put any code that compiles in there and it would simply not run. So why the exception? Plus I'm finding it hard to confirm that it has to do with a prematurely released object, since the program executes fine when moved to willSelectRowAtIndexPath. Maybe there is some object (re)allocation in between the will and did stages?

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        CommentViewController *commentViewController = [[CommentViewController alloc] initWithNibName:@"CommentViewController" bundle:nil];
        Comment *selectedComment = [[Comment alloc] init];
        selectedComment = [self.message.comments objectAtIndex:indexPath.row];
        commentViewController.comment = selectedComment;

        [self presentModalViewController:commentViewController animated:YES];

        [selectedComment release];
        [commentViewController release];    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        NSLog(@"This method is never called ... as I don't see this log output ... ");
}

The Setup:

The UITableView is created as an element in IB, and is wired as a reference variable in the UIViewController. The U开发者_如何学CIViewController is set to be the data source and the delegate of the UITableView. So far so good, the UIViewController view loads, and the UITableView is loaded with the cells correctly. So that means that the delegate methods for the data source have worked properly at least.

Next Step:

So now I want to click on a row and load up another view (either modally or whatever, it doesnt really matter right now) and BOOM, the simulator crashes with the EXC_BAD_ACCESS reported in the console.

Could really use some pointers on this. Thanks a mill for your help in advance!

Matt


didSelectRowAtIndexPath is never being called in the code above because you evoke a modal view in willSelectRowAtIndexPath: which freezes the tableview and keeps it from sending messages to its delegate/datasource.

Here's the cause of your crash:

    Comment *selectedComment = [[Comment alloc] init]; // creates new object 
    selectedComment = [self.message.comments objectAtIndex:indexPath.row]; // assigns a different existing object. Newly created object never used and leaking
    commentViewController.comment = selectedComment; // set to assigned existing

    [self presentModalViewController:commentViewController animated:YES];

    [selectedComment release] // BINGO! you release the assigned object 

Your crash is caused by the BINGO line above. You're confusing the initialized Comment object with the other completely unrelated Comment object returned from the array. You release the Comment object returned from the array without ever having retained it. The object then dies and any attempt to access it subsequently causes the crash.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜