NSRangeException - 索引(2008)超出范围(1)

时间:2014-01-05 16:18:45

标签: ios objective-c uitableview magicalrecord

我试图获取UITableViewCell的内容,然后从Core Data模型中删除它以便对其内容进行操作。如果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)'错误

我最大的困惑是,这只发生在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

2 个答案:

答案 0 :(得分:1)

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

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

继续录制已删除对象的状态或删除它。修复获取该对象的代码,只修改NSLog。获取该对象的代码应该完全匹配cellForRowAtIndexPath:中获取它的代码,并且在那里使用的MOC应该与numberOfRowsInSection:MOC完全匹配。

答案 1 :(得分:0)

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

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

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

在此行尝试断点并添加一些日志记录以显示第0部分中的哪些行正在尝试删除。