尝试删除单元时,使用自定义按钮时遇到此错误。使用自定义UIButton删除UITableViewCell
*终止应用程序由于未捕获的异常 'NSRangeException',原因是: '* - [__ NSArrayM removeObjectAtIndex:]:指数1超出范围[0 .. 0]'
继承人如何我代码是结构化的。我有一个自定义UITableViewCell
,其中包含一个UIButton
,它具有删除单元的操作。删除单元格的操作在我的主视图控制器中,其中包含单元格的实际indexPath
。这个结构工作正常。
继承人删除单元格的操作。
NSIndexPath *indexPath = [self globalIndexPath];
[self.array removeObjectAtIndex:[indexPath row]];
[db deleteTaskAtIndex:[indexPath row]];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationFade];
当我宣布上述indexPath
,则globalIndexPath
属性设置在我cellForRowAtIndexPath
,给它原来indexPath
的价值。我就是这么宣布的。
[self setGlobalIndexPath:indexPath];
现在,我已经在这里下了一些NSLogs
一个有记录的indexPath
。例如在viewWillAppear
方法和viewDidLoad
方法中,都给我准确的indexPath
,我甚至检查了array
输出,并且它们都返回准确的结果,所以我真的不知道为什么它给了我这个错误。
这是我的自定义单元中的代码,用于检测按钮被点击的时间。
NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
if (!notificationArray || !notificationArray.count)
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"DeleteSignal" object:nil];
}
else
{
UILocalNotification *notif = [notificationArray objectAtIndex:[checkbox tag]];
[[UIApplication sharedApplication] cancelLocalNotification:notif];
}
然后我删除使用NSNotificationCenter
用钥匙DeleteSignal
细胞。
尝试设置一个断点,当[indexPath行]是> = [self.array计数]之前每个删除...或者声明该行少于count。另外,如果不刷新表格,你有可能删除多个项目? – 2012-04-03 11:16:01
我会尝试断点,但是,我尝试在我的'viewDidLoad'和'viewWillAppear'中重新加载我的'tableView'。 – Souljacker 2012-04-03 11:21:13
我在我的删除函数前后做了一个if语句,并且放了一个日志,但我不知道如何工作。 – Souljacker 2012-04-03 12:00:37