[英]iPhone-App crashes when a row of UITableView is deleted
我有一個 UITableView,它使用 singleton object 作為數據源。
我已經實現了以下方法以允許用戶刪除 UITableView 中的行。 但是當我單擊刪除按鈕時,應用程序崩潰並出現異常
方法:
-(void)viewDidLoad
{
some initialization code-----
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"Delete" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleEdit:)];
self.navigationItem.rightBarButtonItem = editButton;
[editButton release];
}
-(IBAction)toggleEdit:(id)sender
{
[self.myOrderTable setEditing:!self.myOrderTable.editing animated:YES];
if(self.myOrderTable.editing)
[self.navigationItem.rightBarButtonItem setTitle:@"Done"];
else
{
[self.navigationItem.rightBarButtonItem setTitle:@"Delete"];
}
}
-(void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath
{
NSUInteger row = [indexPath row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
但是,當我嘗試單擊刪除按鈕時出現以下異常:
-[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:995 中的斷言失敗
由於未捕獲的異常“NSInternalInconsistencyException”而終止應用程序,原因:“無效更新:第 1 節中的行數無效。更新 (1) 后現有節中包含的行數必須等於該節中包含的行數更新前的部分 (1),加上或減去從該部分插入或刪除的行數(0 插入,1 刪除)。
*** Call stack at first throw:
(
0 CoreFoundation 0x010305a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x01184313 objc_exception_throw + 44
2 CoreFoundation 0x00fe8ef8
+[NSException raise:format:arguments:] + 136
3 Foundation 0x001153bb -
[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4 UIKit 0x00398e8b -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 8420
5 UIKit 0x00387cf8 -
[UITableView deleteRowsAtIndexPaths:withRowAnimation:] + 56
6 Restaurant 0x000117f9 -
[MyOrderViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 114
7 UIKit 0x00385037 -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] + 101
8 UIKit 0x0031a4fd -[UIApplication sendAction:to:from:forEvent:] + 119
9 UIKit 0x003aa799 -[UIControl sendAction:to:forEvent:] + 67
10 UIKit 0x003acc2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
11 UIKit 0x003ab7d8 -[UIControl touchesEnded:withEvent:] + 458
.............
..........
.............
)
terminate called after throwing an instance of 'NSException'
我可以從哪里開始解決這個問題?
“更新后現有節中包含的行數 (1) 必須等於更新前該節中包含的行數 (1),加上或減去從該節中插入或刪除的行數 (0已插入,已刪除 1 個)。'"
該錯誤清楚地說明了問題所在。 我會檢查你的numberOfRowsInSection
方法。 您需要考慮被刪除的行。 您不能總是返回相同數量的行。 如果您要從表中刪除一行,您的 numberOfRowsInSection 需要返回它的舊返回值減去 1。
例如:
假設我使用一個字符串數組來填充表格視圖;
我的 numberOfRowsInSection 方法將使用
return [array count];
我的 cellForRowAtIndexPath 方法將使用
cell.textLabel.text = (NSString*)[array objectAtIndex:indexPath.row];
當我從 tableView 中刪除一行(比如第 3 行)時,我也會從數組中刪除 object:
[array removeObjectAtIndex:3];
這樣,當再次調用 numberOfRowsInSection 時,數組比之前少一個 object 並且 numberOfRowsInSection 返回一個數字,這說明了刪除的行。
你讀過錯誤信息嗎? 它說:
原因:'無效更新:第 1 節中的行數無效。
所以......你的 tableView 中的行數現在是錯誤的。 為什么?
更新后現有節中包含的行數 (1) 必須等於更新前該節中包含的行數 (1),加上或減去從該節中插入或刪除的行數 (0 插入, 1 已刪除)。
當您從 tableView 中刪除一行時,您還必須從 tableView 中顯示的對象數組中刪除相應的 object。 你沒有那樣做。 因此, UITableView
拋出異常。
您是否在 commitEditingStyle: 中從數據源中刪除項目? 如果表數據源有 2 個項目,並且您在 commitEditingStyle: 中刪除了一行,您還應該從數據源中刪除一個項目,以便現在表視圖和數據源都有 1 個元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.