iPhone应用程序冻结
问题描述:
使用iPhone模拟器2.2.1iPhone应用程序冻结
使用表格并希望将复选标记添加到所选单元格。
我从一个应用程序中添加了这段代码,该应用程序可以工作到不同的应用程序,但使用同样的方法。现在,这个新代码编译并启动,并在选定的单元格中打上复选标记,几秒钟后程序冻结。
*此代码部分来自一个工作应用程序。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
***This snippet was added (from another working app)
if ([[tableView cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark)
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
else
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
[self performSelector:@selector(deselect) withObject:nil afterDelay:0.5f];
***End of snippet
}
我为控制台放置了一些printf语句,执行过程通过if语句并通过最后一行。
所以它没有代码片段,如果我包含它编译,启动的代码片段,并且在选择一个项目并且出现复选标记后,应用程序会冻结。
控制台的错误消息如下。
*** -[RootViewController deselect]: unrecognized selector sent to instance 0x526750
2011-04-16 16:15:30.132 lab3[37268:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[RootViewController deselect]: unrecognized selector sent to instance 0x526750'
答
你发送一个-deselect
消息self
,这显然是RootViewController的实例。但是,您的RootViewController类没有-deselect方法,因此会引发异常,导致错误。因此,要么停止发送-deselect
到self
,要么将-deselect
方法添加到RootViewController。
顺便说一句,搜索NSInvalidArgumentException会出现很多很多类似的问题,因此您不必等待所有答案。
答
答案出现在错误消息中:对于对象RootViewController
,没有选择器deselect
,在您的代码(和代码段)中代表self
。方法deselect
必须是在其他程序中定义的方法,但不在当前应用程序中。从其他程序中剪切并粘贴,我敢打赌解决了这个问题。
另一个提示(与问题无关,但代码如下):如果您在if语句之前声明局部变量,然后在if语句中使用该变量,则可以避免所有对cellForRowAtIndexPath:
的调用。
UITAbleViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell accessoryType] == UITableViewCellAccessoryCheckmark)
....
一旦你解决了其他问题,你的表现会提高。
可能重复的[NSInvalidArgumentException](http://stackoverflow.com/questions/726727/nsinvalidargumentexception) – Caleb 2011-04-16 21:20:01
它真的有帮助,如果你实际上读取错误信息说(什么时候考虑你复制的代码) 。 – Eiko 2011-04-16 21:33:03