在[self.tableView endUpdates]上崩溃

时间:2015-11-25 10:38:51

标签: ios objective-c uitableview

我的tableView有几个部分,第二部分我有NSFetchedResultsController。从第2部分删除行后,我在刷新表格视图时遇到问题。

numberOfRowsInSection(当我加载tableView(count = 0)和添加对象后它运行良好)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    switch (section) {
        case 2:
            return [[[self.fetchedResultsController sections] objectAtIndex:0] numberOfObjects];
        default:
            return 1;
    }
}

controllerWillChangeContent

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView beginUpdates];
}

controller didChangeObject

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
    UITableView *tableView = self.tableView;
    NSIndexPath *frcIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:(indexPath.section -2 )];

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:@[frcIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:frcIndexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

controllerDidChangeContent

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView endUpdates];
}

crash

堆栈跟踪

Stack trace : (
0   Crossfit                            0x000000010267c40d -[MainSceneTableViewController controllerDidChangeContent:] + 61
1   CoreData                            0x000000010312cd08 __77-[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:]_block_invoke + 4632
2   CoreData                            0x000000010303e937 developerSubmittedBlockToNSManagedObjectContextPerform + 199
3   CoreData                            0x000000010303e7fe -[NSManagedObjectContext performBlockAndWait:] + 222
4   CoreData                            0x000000010312bad7 -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] + 119
5   CoreFoundation                      0x000000010347977c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
6   CoreFoundation                      0x00000001034794bb _CFXRegistrationPost + 427
7   CoreFoundation                      0x0000000103479222 ___CFXNotificationPost_block_invoke + 50
8   CoreFoundation                      0x00000001034bea52 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1986
9   CoreFoundation                      0x0000000103374859 _CFXNotificationPost + 633
10  Foundation                          0x000000010272d1e9 -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
11  CoreData                            0x0000000103024736 -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] + 70
12  CoreData                            0x00000001030b507e -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:] + 334
13  CoreData                            0x000000010301f569 -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 2809
14  CoreData                            0x0000000102ff7d44 -[NSManagedObjectContext executeFetchRequest:error:] + 436
15  Crossfit                            0x000000010267793b -[AKDataManager allObjects] + 283
16  Crossfit                            0x000000010267800b -[AKDataManager printAllObjects] + 43
17  Crossfit                            0x000000010267be6f -[MainSceneTableViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 495
18  UIKit                               0x00000001039b0b75 -[UITableView animateDeletionOfRowWithCell:] + 177
19  UIKit                               0x00000001039870dd __52-[UITableView _swipeActionButtonsForRowAtIndexPath:]_block_invoke + 80
20  UIKit                               0x00000001039b254e -[UITableView _actionButton:pushedInCell:] + 172
21  UIKit                               0x0000000103bd0376 -[UITableViewCell _actionButtonPushed:] + 82
22  UIKit                               0x000000010385ae91 -[UIApplication sendAction:to:from:forEvent:] + 92
23  UIKit                               0x00000001039c64d8 -[UIControl sendAction:to:forEvent:] + 67
24  UIKit                               0x00000001039c67a4 -[UIControl _sendActionsForEvents:withEvent:] + 311
25  UIKit                               0x00000001039c58d4 -[UIControl touchesEnded:withEvent:] + 601
26  UIKit                               0x0000000103d2c9ba _UIGestureRecognizerUpdate + 10279
27  UIKit                               0x00000001038c89c0 -[UIWindow _sendGesturesForEvent:] + 1137
28  UIKit                               0x00000001038c9bf6 -[UIWindow sendEvent:] + 849
29  UIKit                               0x00000001038792fa -[UIApplication sendEvent:] + 263
30  UIKit                               0x0000000103853abf _UIApplicationHandleEventQueue + 6844
31  CoreFoundation                      0x00000001033d9011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
32  CoreFoundation                      0x00000001033cef3c __CFRunLoopDoSources0 + 556
33  CoreFoundation                      0x00000001033ce3f3 __CFRunLoopRun + 867
34  CoreFoundation                      0x00000001033cde08 CFRunLoopRunSpecific + 488
35  GraphicsServices                    0x0000000106c68ad2 GSEventRunModal + 161
36  UIKit                               0x000000010385930d UIApplicationMain + 171
37  Crossfit                            0x0000000102683cbf main + 111
38  libdyld.dylib                       0x0000000105bd092d start + 1
39  ???                                 0x0000000000000003 0x0 + 3

删除代码

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        NSIndexPath *frcIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:(indexPath.section - 2)];
        self.index=frcIndexPath;

        AKDataManager* manager=[AKDataManager sharedManager];
        [manager printAllObjects];

        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        [context deleteObject:[self.fetchedResultsController objectAtIndexPath:frcIndexPath]];



        NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您正在从NSFetchedResultsController插入索引路径,但只有一个(2)属于'属于'它。然而,它可能会尝试更新第0部分。

更清楚: 在

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath
     forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath

你从FRC(0)的传入部分中减去2,这使得-2。尝试添加2,这可能会解决。