首页 文章

如何刷新UITableViewController或NSFetchedResultsController?

提问于
浏览
9

我的UITableViewController或NSFetchedResultsController有点问题 . 我不确定问题是什么,但我猜它是UITableViewController .

正如我所说的,我使用NSFetchedResultsController将我的数据填充到UITableViewController中 . 数据按日期排序,并按日期 - 年份显示在部分中,例如2010年5月/ 2010年6月/等等 . 这显示为节 Headers .

现在,当我添加新数据时,它会自动使用当前日期作为默认值,但如果我想使用日期,那么当前不在部分列表中,例如2010年4月(目前列表中的5月和6月),则无法正确显示 . 只有当我退出应用程序并再次启动它时,它才会显示新的部分 Headers ,一切都很好 .

所以我需要能够刷新我的列表或更新它 . 只需将其添加到上下文并更改它似乎不会更新它 .

同样,我不知道我需要更新什么,如果它是我的NSFetchedResultsController对象或UITableViewController .

更新#1:

我只想到这个方法有一个例外:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates.
    [self.tableView endUpdates];

}

这是我移动数据的地方:

- (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:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

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

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

            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            // Reloading the section inserts a new row and ensures that titles are updated appropriately.
            [tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }

}

这种方法几乎取自Apple的CoreDataBooks示例 . 这是错误:

严重的应用错误 . 在调用-controllerDidChangeContent:期间,从NSFetchedResultsController的委托中捕获到异常 . *** - [NSMutableArray removeObjectAtIndex:]:索引1超出边界[0 .. 0] with userInfo(null)

之后,编辑模式(显示删除按钮的位置)不再起作用 .

谢谢 .

3 回答

  • 0

    好的,看起来我找到了解决方案,我只是跳过这些

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

    并且总是这样做

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

    我显然会给出更多逻辑,因为只有当我想在以前不存在的部分中移动数据时才会出现问题 . 在有人能给我一个真正的解决方案之前,我会选择这个 .

  • 15

    在版本1.2的iPhoneCoreDataRecipes中采用Apple的修复,正确的做法是替换

    // Reloading the section inserts a new row and ensures that titles are updated appropriately.
     [tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
    

    [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
    

    (FileMerge是你的朋友!)

    请注意,版本1.2中还有另一个修复,可以防止在创建新条目时出现不稳定的崩溃 . 我已经报告了这个,并在两周内修复了 . 我鼓励您向Apple提交错误报告 . 它们的响应速度远远超出您的想象,您可以期望获得最佳解决方案 .

  • 2

    您需要实现 controller:didChangeSection:atIndex:forChangeType: 委托方法 . 在其中,根据报告的更改类型,将 insertSections:withRowAnimation:deleteSections:withRowAnimation:reloadSections:withRowAnimation: 消息发送到表视图 .

相关问题