如何刷新UITableViewController或NSFetchedResultsController? [英] How to refresh a UITableViewController or NSFetchedResultsController?

查看:108
本文介绍了如何刷新UITableViewController或NSFetchedResultsController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a little problem with my UITableViewController or NSFetchedResultsController. I am not sure which is the problem soure but I guess its the UITableViewController.

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

As I said I use a NSFetchedResultsController to fill my data into a UITableViewController. The data is sorted by date and is also displayed in sections by date-year, e.g. May 2010/June 2010/ and so on. This is displayed as the section header.

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

Now when I add new data, it automatically uses the current date as default, but if I want to use a date, that is currently not in the section list, e.g. April 2010 (currently May and June in the list), then this is not displayed correctly. Only when I quit the app and start it again it will show the new section headers and everything is fine.

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

So I somehow need to be able to refresh my list or update it. By just adding it to the context and changing it does not seem to update it.

同样,我不确定我需要更新什么,如果它是我的NSFetchedResultsController对象或UITableViewController。

Again, I am not sure what I need to update, if it is my NSFetchedResultsController object or the UITableViewController.

更新#1:

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

I just figured that there is an exception in this method :

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

}

这是我移动数据的地方:

This is where I move the data :

- (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示例。这是错误:

This method is pretty much taken from the CoreDataBooks example from Apple. And this is the error :


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

Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. *** -[NSMutableArray removeObjectAtIndex:]: index 1 beyond bounds [0 .. 0] with userInfo (null)

之后,编辑模式(显示删除按钮的位置)不再有效。

And after that, the edit mode (where the delete button appears) does not work anymore.

谢谢。

推荐答案

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

Ok, looks like I found a solution, I just skip these

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

并始终执行此操作。

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

[self.tableView reloadData];

}

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

I will obviously give this some more logic, since the problem only occures when I want to move my data in a section that does not exist before. Until someone can give me a real solution, I will go with this one.

这篇关于如何刷新UITableViewController或NSFetchedResultsController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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