ios - 删除 Tableview NSFetchedResultsController 中的最后一节行

标签 ios objective-c tableview nsfetchedresultscontroller

我正在构建一个通用的 FetchedResultsController 数据源类,用于我所有的 TableViewControllers,其中一些有多个部分,一些没有(像任何项目)。

问题是当 Fetched Results Controller 有多个部分时(sectionNameKeyPath 不为空)如果 TableView 部分只有一行,didChangeObject 上的删除逻辑必须删除整个部分,但是如果在 Fetched Results Controller 定义中仅设置了一个部分 (sectionNameKeyPath = nil),则相同的逻辑不起作用。

有人知道如何在 didChangeObject 上实现通用删除逻辑来防止此类错误吗?

异常:

An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of sections. The number of sections contained in the table view after the update (1) 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, 1 deleted). with userInfo (null)

//Fetched Results Controller Definition on My Model Class
+ (NSFetchedResultsController*)fetchedResultsController
{
    NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:[self.class entityName]];

    //request.predicate = [NSPredicate predicateWithFormat:@"parent = %@", self];
    request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"dueDate" ascending:NO]];

    [request setFetchLimit:50];

    //Generate an error
    return [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:Store.defaultManagedObjectContext sectionNameKeyPath:nil cacheName:nil];

    //Works Fine
    //return [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:Store.defaultManagedObjectContext sectionNameKeyPath:@"shortDueDate" cacheName:nil];
}


//Fetched Results Controller Generic Data Source Class
- (void)controller:(NSFetchedResultsController*)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath*)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath*)newIndexPath
{
    if (type == NSFetchedResultsChangeInsert) {
        [self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    } else if (type == NSFetchedResultsChangeMove) {
        [self.tableView moveRowAtIndexPath:indexPath toIndexPath:newIndexPath];
    } else if (type == NSFetchedResultsChangeDelete) {

        BOOL deleteSection = FALSE;

        //If have only one section and its the last row then delete entire section
        if ((self.tableView.numberOfSections == 1) && ([self.tableView numberOfRowsInSection:[indexPath section]] == 1 )) {
            deleteSection = TRUE;
        }

        if (deleteSection) {
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:YES];
        } else {
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
    } else {
        NSAssert(NO,@"");
    }
}

最佳答案

如果你实现:

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

然后 FRC 将为您确定是否需要插入或删除部分。

关于ios - 删除 Tableview NSFetchedResultsController 中的最后一节行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27346242/

相关文章:

ios - UIImageView 上的点击手势

objective-c - self.object 和 self->object 的区别

iphone - 下载图片和UIButton BG图片

ios - UialertController 仅在 var 达到特定值时显示一次

objective-c - UIView 方向不正确

cocoa - 使用按键从表格 View 中删除条目

iphone - tableviewDataSource 中的内存管理

ios - IOS 中使用 FetchLimit 和 Core Data 无限滚动 TableView

ios - 在 Xcode 崩溃报告中发现问题

ios - 在加载之前为所有索引调用 sizeForItemAtIndexPath - 我很确定我遇到了 UICollectionView 错误或糟糕的设计