嘗試刪除單元時,使用自定義按鈕時遇到此錯誤。使用自定義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