[英]NSRangeException - Index (2008) beyond bounds (1)
I am trying to get the contents of my UITableViewCell before I delete it from my Core Data model in order to act on its contents. 我试图从UI DataView中删除UITableViewCell的内容,以便对其内容进行操作。 If there is only one item in the Core Data model and I go to delete it from my UITableView, the app will only sometimes throw an error of 2014-01-05 11:10:26.189 Nibbles[43609:70b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFArray objectAtIndex:]: index (2008) beyond bounds (1)'
如果Core Data模型中只有一项,而我要从UITableView中删除它,则该应用有时只会抛出2014-01-05 11:10:26.189 Nibbles[43609:70b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFArray objectAtIndex:]: index (2008) beyond bounds (1)'
My biggest confusion is that this only happens with one item in the UITableView and doesn't happen 100% of the time. 我最大的困惑是,这仅发生在UITableView中的一项上,而并非100%的情况下发生。 If you need to see any other code, please let me know. 如果您需要查看其他任何代码,请告诉我。
Here is the code I am using. 这是我正在使用的代码。 The bolded line is the one causing the error since it never makes it past there to NSLog to the console. 粗体线是导致错误的那一行,因为它永远不会使它流到NSLog控制台。
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(@"Delete button pressed!");
FoodListCell *cell = (FoodListCell*)[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"DEBUG | Selected Cell: %@", cell);
NSString *foodOrActivity = cell.foodNameLabel.text;
NSString *points = cell.foodPointsLabel.text;
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
[[self.fetchedResultsController objectAtIndexPath:indexPath] MR_deleteInContext:localContext];
[localContext MR_saveOnlySelfAndWait];
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
NSManagedObject *deleteObject = [_fetchedResultsController objectAtIndexPath:path];
[managedObjectContext deleteObject:deleteObject];
}
And here is the full stack trace: 这是完整的堆栈跟踪:
2014-01-05 11:10:26.189 Nibbles[43609:70b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFArray objectAtIndex:]: index (2008) beyond bounds (1)'
*** First throw call stack:
(
0 CoreFoundation 0x01cd45e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01a578b6 objc_exception_throw + 44
2 CoreFoundation 0x01cd43bb +[NSException raise:format:] + 139
3 CoreData 0x00280755 -[_PFArray objectAtIndex:] + 133
4 CoreData 0x002f9778 -[_PFMutableProxyArray objectAtIndex:] + 120
5 CoreData 0x00382c1f -[NSFetchedResultsController objectAtIndexPath:] + 255
6 Nibbles 0x0000660d -[FoodListViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 685
7 UIKit 0x008b5ba3 -[UITableView animateDeletionOfRowWithCell:] + 107
8 UIKit 0x00a35695 -[UITableViewCell _swipeDeleteButtonPushed] + 70
9 libobjc.A.dylib 0x01a69874 -[NSObject performSelector:withObject:withObject:] + 77
10 UIKit 0x007c70c2 -[UIApplication sendAction:to:from:forEvent:] + 108
11 UIKit 0x007c704e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
12 UIKit 0x008bf0c1 -[UIControl sendAction:to:forEvent:] + 66
13 UIKit 0x008bf484 -[UIControl _sendActionsForEvents:withEvent:] + 577
14 UIKit 0x008be733 -[UIControl touchesEnded:withEvent:] + 641
15 UIKit 0x00b39c7f _UIGestureRecognizerUpdate + 7166
16 UIKit 0x0080419a -[UIWindow _sendGesturesForEvent:] + 1291
17 UIKit 0x008050ba -[UIWindow sendEvent:] + 1030
18 UIKit 0x007d8e86 -[UIApplication sendEvent:] + 242
19 UIKit 0x007c318f _UIApplicationHandleEventQueue + 11421
20 CoreFoundation 0x01c5d83f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
21 CoreFoundation 0x01c5d1cb __CFRunLoopDoSources0 + 235
22 CoreFoundation 0x01c7a29e __CFRunLoopRun + 910
23 CoreFoundation 0x01c79ac3 CFRunLoopRunSpecific + 467
24 CoreFoundation 0x01c798db CFRunLoopRunInMode + 123
25 GraphicsServices 0x0285e9e2 GSEventRunModal + 192
26 GraphicsServices 0x0285e809 GSEventRun + 104
27 UIKit 0x007c5d3b UIApplicationMain + 1225
28 Nibbles 0x000236ad main + 141
29 libdyld.dylib 0x031b470d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
The conventional pattern is to access the model (not through a table cell, but directly from the model). 常规模式是访问模型(不是通过表格单元格,而是直接从模型中访问)。 Do whatever you need to do with it, then remove it from your model, then remove it from your table... 做任何您需要做的事情,然后从模型中删除,然后从表中删除...
The error implies that the localContext being accessed this method is different from (or is in a different state than) the MOC that reports the number of items in the model (used in numberOfRowsInSection
:) 该错误表示正在访问此方法的localContext与报告模型中项目数的MOC不同(或处于不同的状态)(用于numberOfRowsInSection
:)
Before moving on to recording that deleted object's state, or deleting it. 在继续记录已删除对象的状态或删除它之前。 Fix the code that gets that object and just NSLog it. 修复获取该对象的代码,然后对其进行NSLog。 The code that gets that object should exactly match the code that gets it in your cellForRowAtIndexPath
:, and the MOC used there should exactly match the numberOfRowsInSection
: MOC. 获取该对象的代码应该与您在cellForRowAtIndexPath
:中获取该对象的代码完全匹配 ,并且在那里使用的MOC应该与numberOfRowsInSection
:MOC完全匹配。
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
NSManagedObject *deleteObject = [_fetchedResultsController objectAtIndexPath:path];
[managedObjectContext deleteObject:deleteObject];
Here you are getting the index path for the row in section 0 and deleting it. 在这里,您将获取第0节中该行的索引路径并将其删除。
You already have the index path for the row/cell/section that you want to delete, so why are you trying to delete that row only in section 0? 您已经具有要删除的行/单元格/节的索引路径,那么为什么要尝试仅在第0节中删除该行?
Try a breakpoint at this line and add some logging to show what row in section 0 is trying to be deleted. 在此行尝试一个断点,并添加一些日志记录以显示试图删除第0节中的哪一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.