我有以下代码示例:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[namesArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
和
if ([nameSection count] == 0)
{
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView beginUpdates];
[indexKeys removeObjectAtIndex:section];
[tableView deleteSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
注意顺序....在第一个示例中,我首先从数组中删除对象,然后从 tableview 中删除该行。
在第二个示例中,我从 tableview 中删除行,然后从我的数组中删除部分,然后从 tableview 中删除部分。
这是做事的正确顺序吗?或者我应该先从 UI 中删除它,然后再从数组中删除它?
我问的原因是因为我有一些用户报告了这些我似乎无法重现的崩溃:
NSInternalInconsistencyException - 无效更新:第 1 节中的行数无效。更新 (0) 后现有节中包含的行数必须等于该节中包含的行数之前
使用此堆栈跟踪:
1: libobjc.A.dylib objc_exception_throw 32
2: CoreFoundation [NSException raise:format:] 0
3: Foundation -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] 90
4: UIKit -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] 6042
5: UIKit -[UITableView _updateRowsAtIndexPaths:updateAction:withRowAnimation:] 254
6: UIKit -[UITableView deleteRowsAtIndexPaths:withRowAnimation:] 26
7: Movies _mh_execute_header 253319
8: UIKit -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] 84
9: CoreFoundation -[NSObject performSelector:withObject:withObject:] 52
10: UIKit -[UIApplication sendAction:to:from:forEvent:] 62