问题描述
我从iPhone开发开始,对UITableView进行编程时遇到了一些问题,该UITableView是常规UIViewController的子部分.
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.
问题:
点击一行时,didSelectRowAtIndexPath会生成一个EXC_BAD_ACCESS(我已经检查了正确的方法签名.尽管我将此方法的主体移到willSelectRowAtIndexPath上也能正常工作.这对我来说是错误的,因为所有文档/模式使用didSelectRowAtIndexPath.好吧,所以真正的怪异是是否调用了该方法.当然,如果没有记录以下日志输出,则该方法没有运行,因此我可以将任何编译的代码放在其中,而根本不会那为什么要例外呢?另外我发现很难确认它与过早释放的对象有关,因为当移至willSelectRowAtIndexPath时程序执行得很好,也许在遗嘱之间有一些对象(重新)分配和阶段?
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 ... ");
}
设置:
UITableView在IB中被创建为元素,并在UIViewController中被连接为参考变量.UIViewController设置为数据源和UITableView的委托.到目前为止,UIViewController视图已加载,并且UITableView已正确加载了单元格.因此,这意味着数据源的委托方法至少可以正常工作.
The UITableView is created as an element in IB, and is wired as a reference variable in the UIViewController. The UIViewController 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.
下一步:
所以现在我想单击一行并加载另一个视图(无论是模态还是其他,现在都没有关系)和BOOM,模拟器崩溃,控制台中报告了EXC_BAD_ACCESS.
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!
马特
推荐答案
didSelectRowAtIndexPath
,因为您在 willSelectRowAtIndexPath:
中调用了模式视图冻结表视图,并阻止它向其委托/数据源发送消息.
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.
这是导致崩溃的原因:
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
您的当机是由上面的BINGO行引起的.您正在将初始化的 Comment
对象与从数组返回的另一个完全不相关的 Comment
对象混淆.您释放了从数组返回的 Comment
对象,而没有保留它.然后该对象死亡,随后对其进行任何尝试访问都将导致崩溃.
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.
这篇关于didSelectRowAtIndexPath生成EXC_BAD_ACCESS,而willSelectRowAtIndexPath可以正常工作UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!