多个NSFetchedResultsController - didChangeObject [英] Multiple NSFetchedResultsController - didChangeObject

查看:103
本文介绍了多个NSFetchedResultsController - didChangeObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView 它使用2 NSFetchedResultsControllers 。每个 NSFetchedResultsController 只有一个部分。但是,表有4个部分。我使用 NSFetchedResultsControllers 之一的结果填充表的第四部分。一切工作正常到目前为止。但是如果用户删除第一部分上的第一个单元格,则会更改 NSFetchedResultsControllers 。表的最后一部分中的行可能会被删除。当这个方法被调用:

I have a UITableView which uses 2 NSFetchedResultsControllers. Each NSFetchedResultsController has only one section. However, the table has 4 sections. I populate the 4th section of the table with the results of one of the NSFetchedResultsControllers. Everything works fine so far. But if the user deletes the first cell on the first section, the NSFetchedResultsControllers are changed. The rows in the last section of the table might be deleted. When this method is called:

- (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:
        NSLog(@"section: %d, row: %d", [newIndexPath section], [newIndexPath row]);

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;
...
}

该节始终为0,的 NSFetchedResultsControllers 。因此,该部分与表视图上的正确部分不匹配。

The section is always 0 because it's the section of the NSFetchedResultsControllers. Thus, the section doesn't match the correct one on the table view.

有解决方法吗?我基本上想改变 NSFetchedResultsController 的部分为3而不是0.

Is there a workaround? I would basically like to change the section of a NSFetchedResultsController to 3 instead of 0.

推荐答案

我发现了一个解决方法,但是有一个更漂亮的解决方案是很好的。

I found a workaround, but it would be nice to have a prettier solution.

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
   atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
  newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
if (newIndexPath != nil && controller == self.fetchedXController) {
    newIndexPath = [NSIndexPath indexPathForRow:[newIndexPath row] inSection:3];
    if ([tableView cellForRowAtIndexPath:newIndexPath] == nil) {
        type = NSFetchedResultsChangeInsert;
    }
}
if (indexPath != nil && controller == self.fetchedDomainsController) {
    indexPath = [NSIndexPath indexPathForRow:[indexPath row] inSection:3];
}

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:newIndexPath] atIndexPath:newIndexPath];
        break;
...

这篇关于多个NSFetchedResultsController - didChangeObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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