我在滑动删除后开始收到崩溃报告,并且能够重新创建它。然后因为我使用 Apple 的 iPhoneCoreDataRecipes 示例作为模型,所以我尝试了原始示例代码并且每次都崩溃。
要重新创建,只需在 CoreDataRecipes 下载并运行项目.然后 (1) 点击,比方说,杏仁饼来查看食谱中的成分,(2) 轻扫以删除一种成分,(3) 轻扫另一种成分。崩溃。
我的问题是:我是否遗漏了示例项目代码中的错误,或者这实际上是操作系统中的错误?其次,是否有一种解决方法可以识别这种情况并防御它,但仍然允许滑动删除? (目前,我只是通过从 tableView:editingStyleForRowAtIndexPath: 返回 UITableViewCellEditingStyleNone 来禁用滑动删除,除非我已经处于编辑模式。)
在这样的列表中,“编辑”按钮向 tableVC 发送一个 setEditing:YES,它在新行中添加一个“添加项目”按钮(显然允许向列表添加新项目),以及现有的项目添加红色减号图标以允许删除。完成后,Done 发送 setEditing:NO,删除新行和图标。一切正常。 (请参阅下面调试输出中的案例 1)。
如果用户只是想删除单个项目,他们可以通过滑动该项目来完成(并通过点击删除来确认)。为了保持一致,滑动还向 tableVC 发送一个 setEditing:YES,然后 Delete 按钮同时发送一个 commitEditingStyle:delete 消息和一个 setEditing:NO 消息。问题是系统在这两条消息之间询问表中的行数,而不是在 setEditing:NO 之后。因此它记得比实际多了一行。然后,如果您对表格执行任何操作(例如再次点击编辑,再次插入添加行),它会看到不一致的行数,然后崩溃。请参阅下面输出中的案例 2。
顺便说一句,我认为如果您滑动删除,但改变主意并点击“完成”(而不是点击“删除”以外的任何地方),则会出现第二个错误。它再次递归调用 setEditing:YES。请参阅下面输出中的案例 3
=============
>>CASE 1: Using Edit control to setEditing
2013-12-29 11:02:04.032 Recipes[10287:70b] Num Rows: 4
2013-12-29 11:02:04.035 Recipes[10287:70b] Num Rows: 4
-Hit Edit
2013-12-29 11:02:14.720 Recipes[10287:70b] setEditing: On
2013-12-29 11:02:14.725 Recipes[10287:70b] Inserting Add row 4
2013-12-29 11:02:14.726 Recipes[10287:70b] Num Rows: 4 +1
2013-12-29 11:02:14.727 Recipes[10287:70b] Providing Add Cell for row 4
-Hit EditControl ("-") then Delete
2013-12-29 11:02:21.806 Recipes[10287:70b] Deleting ingredient at row 2
2013-12-29 11:02:21.808 Recipes[10287:70b] Num Rows: 3 +1
-Hit Done
2013-12-29 11:02:28.032 Recipes[10287:70b] setEditing: Off
2013-12-29 11:02:28.036 Recipes[10287:70b] Deleting Add row 3
2013-12-29 11:02:28.036 Recipes[10287:70b] Num Rows: 3
CASE 2: Using Swipe to Delete
2013-12-29 11:03:21.705 Recipes[10304:70b] Num Rows: 4
2013-12-29 11:03:21.707 Recipes[10304:70b] Num Rows: 4
-Swipe on row 2
2013-12-29 11:03:34.357 Recipes[10304:70b] setEditing: On
2013-12-29 11:03:34.358 Recipes[10304:70b] Inserting Add row 4
2013-12-29 11:03:34.359 Recipes[10304:70b] Num Rows: 4 +1
2013-12-29 11:03:34.359 Recipes[10304:70b] Providing Add Cell for row 4
-Hit Delete
2013-12-29 11:03:38.427 Recipes[10304:70b] Deleting ingredient at row 2
2013-12-29 11:03:38.431 Recipes[10304:70b] Num Rows: 3 +1
2013-12-29 11:03:38.432 Recipes[10304:70b] setEditing: Off
2013-12-29 11:03:38.434 Recipes[10304:70b] Deleting Add row 3 NOTE THAT IT DOES >>NOT<< CALL numberOfRows during this delete, but complains that current numberOfRows is wrong.
2013-12-30 23:54:26.349 Recipes[16694:70b] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2935.80.1/UITableView.m:1366
2013-12-30 23:54:26.352 Recipes[16694:70b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack:
(
0 CoreFoundation 0x01b48c94 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x018c78b6 objc_exception_throw + 44
2 CoreFoundation 0x01b48af8 +[NSException raise:format:arguments:] + 136
3 Foundation 0x005aa1ae -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4 UIKit 0x00ab3463 -[UITableView _endCellAnimationsWithContext:] + 13402
5 UIKit 0x00ac313a -[UITableView endUpdatesWithContext:] + 51
6 UIKit 0x00ac3168 -[UITableView endUpdates] + 41
7 Recipes 0x00009a8c -[RecipeDetailViewController setEditing:animated:] + 908
8 UIKit 0x00cab0b5 -[UITableViewController tableView:willBeginEditingRowAtIndexPath:] + 49
9 UIKit 0x00abbb39 -[UITableView _sendWillBeginEditingForIndexPath:] + 73
10 UIKit 0x00ac772d -[UITableView _swipeCellAtPoint:] + 346
11 UIKit 0x00c44cd0 -[UITableViewCellScrollView gestureRecognizerShouldBegin:] + 354
12 UIKit 0x00d6d676 -[UIGestureRecognizer _shouldBegin] + 1309
13 UIKit 0x00d69f95 -[UIGestureRecognizer setState:] + 171
14 UIKit 0x00d9a9d7 -[UIPanGestureRecognizer touchesMoved:withEvent:] + 142
15 UIKit 0x00e4dd5a -[UIScrollViewPanGestureRecognizer touchesMoved:withEvent:] + 89
16 UIKit 0x00a1c148 -[UIWindow _sendGesturesForEvent:] + 649
17 UIKit 0x00a1d2e1 -[UIWindow sendEvent:] + 1021
18 UIKit 0x009eff32 -[UIApplication sendEvent:] + 242
19 UIKit 0x009d9ec3 _UIApplicationHandleEventQueue + 11455
20 CoreFoundation 0x01ad1f5f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
21 CoreFoundation 0x01ad18eb __CFRunLoopDoSources0 + 235
22 CoreFoundation 0x01aee9be __CFRunLoopRun + 910
23 CoreFoundation 0x01aee1e3 CFRunLoopRunSpecific + 467
24 CoreFoundation 0x01aedffb CFRunLoopRunInMode + 123
25 GraphicsServices 0x02f5a2c2 GSEventRunModal + 192
26 GraphicsServices 0x02f5a0e9 GSEventRun + 104
27 UIKit 0x009dcabb UIApplicationMain + 1225
28 Recipes 0x0000257d main + 125
29 Recipes 0x000024f5 start + 53
)
>>CASE 3: Using Swipe to Delete but then not deleting, with "Done"
2013-12-29 11:05:20.802 Recipes[10319:70b] Num Rows: 4
2013-12-29 11:05:20.805 Recipes[10319:70b] Num Rows: 4
-Swipe on row 2
2013-12-29 11:05:25.166 Recipes[10319:70b] setEditing: On
2013-12-29 11:05:25.167 Recipes[10319:70b] Inserting Add row 4
2013-12-29 11:05:25.168 Recipes[10319:70b] Num Rows: 4 +1
2013-12-29 11:05:25.168 Recipes[10319:70b] Providing Add Cell for row 4
-Hit Done
2013-12-29 11:05:27.685 Recipes[10319:70b] setEditing: Off
2013-12-29 11:05:27.686 Recipes[10319:70b] setEditing: Off
Breakpoint at beginning of setEditingAnimated shows:
#0 0x00008edb in -[RecipeDetailViewController setEditing:animated:] at /Users/hugh/Downloads/iPhoneCoreDataRecipes-2/Classes/RecipeDetailViewController.m:150
#1 0x00c90f1b in -[UITableViewController tableView:didEndEditingRowAtIndexPath:] ()
#2 0x00aadcbb in -[UITableView _sendDidEndEditingForIndexPath:] ()
#3 0x00ac4a1f in -[UITableView _endSwipeToDeleteRowDidDelete:] ()
#4 0x00abbdbb in -[UITableView _setEditing:animated:forced:] ()
#5 0x00abbf58 in -[UITableView setEditing:animated:] ()
#6 0x00c90982 in -[UITableViewController setEditing:animated:] ()
#7 0x00008f16 in -[RecipeDetailViewController setEditing:animated:] at /Users/hugh/Downloads/iPhoneCoreDataRecipes-2/Classes/RecipeDetailViewController.m:150
#8 0x00afc3ae in -[UIViewController(UINavigationControllerItem) _toggleEditing:] ()
#9 0x01853874 in -[NSObject performSelector:withObject:withObject:] ()
#10 0x009d60c2 in -[UIApplication sendAction:to:from:forEvent:] ()
#11 0x00caac9b in -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] ()
NOTE THE RECURSIVE CALL TO setEditing:animated:
AND CONTINUING LEADS TO THE CRASH:
2013-12-29 11:05:27.688 Recipes[10319:70b] Deleting Add row 4
2013-12-29 11:05:27.689 Recipes[10319:70b] Num Rows: 4
2013-12-29 11:05:27.693 Recipes[10319:70b] Deleting Add row 4
2013-12-29 11:05:27.694 Recipes[10319:70b] Num Rows: 4
2013-12-29 11:05:27.694 Recipes[10319:70b] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2903.23/UITableView.m:1076
2013-12-29 11:05:27.697 Recipes[10319:70b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 4 from section 1 which only contains 4 rows before the update'
*** First throw call stack:
(
0 CoreFoundation 0x01abe5e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x018418b6 objc_exception_throw + 44
2 CoreFoundation 0x01abe448 +[NSException raise:format:arguments:] + 136
3 Foundation 0x005a3fee -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4 UIKit 0x00aa3373 -[UITableView _endCellAnimationsWithContext:] + 3952
5 UIKit 0x00ab4caa -[UITableView endUpdatesWithContext:] + 51
6 UIKit 0x00ab4cd8 -[UITableView endUpdates] + 41
7 Recipes 0x000091fc -[RecipeDetailViewController setEditing:animated:] + 908
8 UIKit 0x00afc3ae -[UIViewController(UINavigationControllerItem) _toggleEditing:] + 73
9 libobjc.A.dylib 0x01853874 -[NSObject performSelector:withObject:withObject:] + 77
10 UIKit 0x009d60c2 -[UIApplication sendAction:to:from:forEvent:] + 108
11 UIKit 0x00caac9b -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 139
12 libobjc.A.dylib 0x01853874 -[NSObject performSelector:withObject:withObject:] + 77
13 UIKit 0x009d60c2 -[UIApplication sendAction:to:from:forEvent:] + 108
14 UIKit 0x009d604e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
15 UIKit 0x00ace0c1 -[UIControl sendAction:to:forEvent:] + 66
16 UIKit 0x00ace484 -[UIControl _sendActionsForEvents:withEvent:] + 577
17 UIKit 0x00acd733 -[UIControl touchesEnded:withEvent:] + 641
18 UIKit 0x00a1351d -[UIWindow _sendTouchesForEvent:] + 852
19 UIKit 0x00a14184 -[UIWindow sendEvent:] + 1232
20 UIKit 0x009e7e86 -[UIApplication sendEvent:] + 242
21 UIKit 0x009d218f _UIApplicationHandleEventQueue + 11421
22 CoreFoundation 0x01a4783f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
23 CoreFoundation 0x01a471cb __CFRunLoopDoSources0 + 235
24 CoreFoundation 0x01a6429e __CFRunLoopRun + 910
25 CoreFoundation 0x01a63ac3 CFRunLoopRunSpecific + 467
26 CoreFoundation 0x01a638db CFRunLoopRunInMode + 123
27 GraphicsServices 0x02eae9e2 GSEventRunModal + 192
28 GraphicsServices 0x02eae809 GSEventRun + 104
29 UIKit 0x009d4d3b UIApplicationMain + 1225
30 Recipes 0x00001ced main + 125
31 Recipes 0x00001c65 start + 53
最佳答案
我还不确定这是 CoreDataRecipes 程序的错误还是 iOS 7 中的错误, 但以下似乎有效:
在 tableView:commitEditingStyle:editingStyle:forRowAtIndexPath:
中,在滑动删除的情况下,我们知道此时 'self.editing' 为 NO,因此如果您向'self.editing' 在 deleteRows 之后,tableView 似乎更新了它的内部 numberOfRows
。具体来说,在:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
添加:
if (!self.editing) self.editing = NO;
其次,在 setEditing:animated:
中,检查附加的“Add Item”行在删除之前是否确实存在。具体来说,替换:
} else {
[self.tableView deleteRowsAtIndexPaths:ingredientsInsertIndexPath withRowAnimation:UITableViewRowAnimationTop];
overviewTextField.placeholder = @"";
}
与:
} else {
if (ingredientsCount < [self.tableView numberOfRowsInSection:INGREDIENTS_SECTION]) {
[self.tableView deleteRowsAtIndexPaths:ingredientsInsertIndexPath withRowAnimation:UITableViewRowAnimationTop];
}
overviewTextField.placeholder = @"";
}
这似乎可以解决您的两个问题。
关于ios - 为什么 Swipe-to-Delete 在 iOS 7 下会崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21368308/