带有NSFetchedResultsController的表移动了添加部分的行 [英] Table with NSFetchedResultsController move row with adding section

查看:89
本文介绍了带有NSFetchedResultsController的表移动了添加部分的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有UFableView和NSFetchedResultsController

I have UITableView with NSFetchedResultsController

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc]
                                                             initWithFetchRequest:[self dataFetchRequest]
                                                             managedObjectContext:[MLCoreDataService sharedInstance].managedObjectContext
                                                             sectionNameKeyPath:@"checked"
                                                             cacheName:nil];
aFetchedResultsController.delegate = self;

我已实施

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
    UITableView *tableView = self.tableView;

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

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

        case NSFetchedResultsChangeUpdate:
            [(MLItemTableViewCell*)[tableView cellForRowAtIndexPath:indexPath] setItem:anObject];
            break;

        case NSFetchedResultsChangeMove:

            [tableView moveRowAtIndexPath:indexPath toIndexPath:newIndexPath];
            break;
    }

    [self.tableView reloadData];
}

当我更改我行中实体的已检查属性时,我接到了一个电话到

When I change "checked" property of entity in my row I got a call to

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

with类型NSFetchedResultsChangeMove

with type NSFetchedResultsChangeMove

在编辑之前和之后我有相同的部分数量时,所有都是正确的。
但是当我只有一个部分并且在编辑行之后应该转到下一部分我得到以下错误

All is right when I have the same sections amount before editing and after. But when I have only one section and after editing row should go to the next section I got the following error

2014-07-27 17:37:43.384 mlist[34340:60b] CoreData: error: Serious application error.  Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.  Invalid update: invalid number of sections.  The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted). with userInfo (null)
2014-07-27 17:37:43.420 mlist[34340:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections.  The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

当我尝试在didChangeObject上插入删除...类型为NSFetchedResultsChangeMove的方法我与部分行数有相同的错误。

When I try to insert of delete on didChangeObject... method with type NSFetchedResultsChangeMove I have the same error with numbers of rows in sections.

是否有通用的解决方法?什么是正确的方法?

Is there common way to resolve it? And what the right way to do it?

推荐答案

通过自定义调用解决问题

Problem resolved by custom call of

[self.tableView beginUpdates];
[self.tableView endUpdates];

NSFetchedResultsControllerDelegate的整个代码

Whole code of NSFetchedResultsControllerDelegate

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
    UITableView *tableView = self.tableView;

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

        case NSFetchedResultsChangeDelete:{
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
        break;

        case NSFetchedResultsChangeUpdate:{
            [(MLItemTableViewCell*)[tableView cellForRowAtIndexPath:indexPath] setItem:anObject];
        }
        break;

        case NSFetchedResultsChangeMove:{
            if (_sectionsChanged) {
                [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];

                _sectionsChanged = NO;
            } else {
                [tableView moveRowAtIndexPath:indexPath toIndexPath:newIndexPath];
            }
        }
        break;
    }

}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                          withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                          withRowAnimation:UITableViewRowAnimationFade];
            break;
    }

    _sectionsChanged = YES;
}

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

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

这篇关于带有NSFetchedResultsController的表移动了添加部分的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆