NSRangeException - Index (2008) beyond bounds (1)



我正在尝试获取UITableViewCell的内容,然后再将其从核心数据模型中删除,以便对其内容进行操作。如果核心数据模型中只有一个项目,并且我从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)'错误

我最大的困惑是,这只发生在UITableView中的一个项目上,而不是100%的时间。如果您需要查看任何其他代码,请告诉我。

这是我正在使用的代码。粗体行是导致错误的行,因为它永远不会通过那里到 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];
    }

这是完整的堆栈跟踪:

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

常规模式是访问模型(不是通过表格单元格,而是直接从模型)。 做任何你需要用它做的事情,然后从你的模型中删除它,然后从你的桌子上删除它......

该错误意味着正在访问此方法的 localContext 与报告模型中项数的 MOC 不同(或处于不同的状态numberOfRowsInSection :)

在继续记录已删除对象的状态或删除它之前。 修复获取该对象的代码,然后只需对其进行 NSLog。 获取该对象的代码应与在 cellForRowAtIndexPath 中获取该对象的代码完全匹配:,并且那里使用的 MOC 应与 numberOfRowsInSection :MOC 完全匹配。

NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
NSManagedObject *deleteObject = [_fetchedResultsController objectAtIndexPath:path];
[managedObjectContext deleteObject:deleteObject];

在这里,您将获取第 0 节中行的索引路径并将其删除。

已经拥有要删除的行/单元格/部分的索引路径,那么为什么您尝试仅在第 0 节中删除该行?

尝试在此行处使用断点,并添加一些日志记录以显示第 0 节中尝试删除的行。

相关内容

最新更新