如何刷新UITableViewController或NSFetchedResultsController?

ele*_*nse 9 iphone uitableview nsfetchedresultscontroller

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

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

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

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

同样,我不知道我需要更新什么,如果它是我的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];

}
Run Code Online (Sandbox Code Playgroud)

这是我移动数据的地方:

- (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;
    }

}
Run Code Online (Sandbox Code Playgroud)

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

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

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

谢谢.

ele*_*nse 15

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

[self.tableView beginUpdates];
[self.tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)

并且总是这样做

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{

[self.tableView reloadData];

}
Run Code Online (Sandbox Code Playgroud)

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

  • 我在WWDC上与一位苹果工程师谈过此事.他说这是UITableView中的一个错误(已在4.0中修复).如果你在<4.0,那就像这样调用`reloadData`.如果你在> = 4.0,那就像文档说的那样. (3认同)

Ole*_*ann 0

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