9

我的 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];

}

这是我移动数据的地方:

- (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] 与 userInfo (null)

之后,编辑模式(出现删除按钮的位置)不再起作用。

谢谢。

4

3 回答 3

15

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

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

并始终这样做。

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

[self.tableView reloadData];

}

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

于 2010-06-19T23:55:17.920 回答
2

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

 // 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 提交错误报告。它们的响应速度比您想象的要快得多,您可以期望得到最好的解决方案。

于 2010-10-04T14:18:38.190 回答
0

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

于 2010-06-19T22:20:14.983 回答