XCode 9.0:应用程序在删除表格单元格上崩溃

时间:2017-10-11 16:57:54

标签: ios objective-c xcode uitableview

我有一个UITable。我能够成功地对表格进行编辑,以便您可以滑动以删除单元格。但是,自从更新我的Xcode后,当我滑动删除时,我的应用程序崩溃了。

Thread 1
  2 - [ViewController configureCell:atIndexPath:]
  3 - [ViewController controller:didChangeObject:atIndexPathforChangeType:newIndexPath];
  16 - [ViewController tableView:commitEditingStyle:forRowAndIndexPath];

查看上面的线程,16在if子句导致断点,在保存核心数据对象的上下文后检查错误。然后第3行显示NSFetchRequestUpdate被触发而不是NSFetchRequestDelete - 所以应用程序尝试配置单元格而不是删除它。 为什么在更新我的xCode后会发生这种情况?此question有类似的问题,但没有经过验证的答案。

这是我删除单元格的代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*Only allow deletion for collection table */
    if(_segmentedControl.selectedSegmentIndex == 1) {
        NSLog(@"delete ca");
        if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        CollectedLeaf* collectedLeaf = [collectionFetchedResultsController objectAtIndexPath:indexPath];
        LeafletPhotoUploader * leafletPhotoUploader = [[LeafletPhotoUploader alloc] init];
        leafletPhotoUploader.collectedLeaf = collectedLeaf;

        if([LeafletUserRegistration isUserRegistered]) {
            [leafletPhotoUploader deleteCollectedLeaf:collectedLeaf delegate:self];
        }


        // Delete the managed object for the given index path
        NSManagedObjectContext *context = [collectionFetchedResultsController managedObjectContext];
        [context deleteObject:[collectionFetchedResultsController objectAtIndexPath:indexPath]];

        // Save the context.
        NSError *error;
        if (![context save:&error]) //this causes breakpoint
        {
            NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
            NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
            if(detailedErrors != nil && [detailedErrors count] > 0)
            {
                for(NSError* detailedError in detailedErrors)
                {
                    NSLog(@"  DetailedError: %@", [detailedError userInfo]);
                }
            }
            else 
            {
                NSLog(@"  %@", [error userInfo]);
            }
        }
    }

    }

}

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

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [_table insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                          withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            NSLog(@"delete called");
            [_table beginUpdates];
            [_table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                          withRowAnimation:UITableViewRowAnimationFade];
            [_table endUpdates];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[_table cellForRowAtIndexPath:indexPath]
                    atIndexPath:indexPath];
            NSLog(@"fetched update");
            break;

        case NSFetchedResultsChangeMove:
            [_table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                          withRowAnimation:UITableViewRowAnimationFade];
            [_table insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                          withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

0 个答案:

没有答案
相关问题