【问题标题】:moveRowAtIndexPath in UITableView causes incorrect animationUITableView 中的 moveRowAtIndexPath 导致动画不正确
【发布时间】:2011-05-12 22:26:48
【问题描述】:

我有一个显示 CoreData 的简单 UITableView 控制器。我正在尝试实现 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;并且动画有问题。 Core Data 存储已更新,但动画无法正常工作。

如何让动画正确反映核心数据对象发生的变化?

例如:

初始顺序:

在第 2 项之后到顶部:

或者,初始顺序:

将项目 1 移动到位置 3 后:

以下是相关代码:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
//this implementation is from this tutorial: http://www.cimgf.com/2010/06/05/re-ordering-nsfetchedresultscontroller/
NSMutableArray *things = [[fetchedResultsController fetchedObjects] mutableCopy];

// Grab the item we're moving.
NSManagedObject *thing = [fetchedResultsController objectAtIndexPath:fromIndexPath];

// Remove the object we're moving from the array.
[things removeObject:thing];
// Now re-insert it at the destination.
[things insertObject:thing atIndex:[toIndexPath row]];

// All of the objects are now in their correct order. Update each
// object's displayOrder field by iterating through the array.


int i = 0;
for (NSManagedObject *mo in things)
{
    [mo setValue:[NSNumber numberWithInt:i++] forKey:@"order"];
}


NSLog(@"things: %@", things);
[things release], things = nil; 

[managedObjectContext save:nil];            
}

和代表:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
NSLog(@"didChangeObject:");
UITableView *tableView = self.tableView;

switch(type) {
    case NSFetchedResultsChangeInsert:
        NSLog(@"ResultsChangeInsert:");
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;

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

    case NSFetchedResultsChangeMove:
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;
}
}

【问题讨论】:

    标签: iphone cocoa-touch core-data uitableview


    【解决方案1】:

    问题是由于代理干扰了我的重新排序实现。 我在保存 ManagedObjectContext 之前添加了一个布尔值:

    重新排序 = 是; [managedObjectContext 保存:无];

    并使用它跳过任何 FetchedResultsController 委托函数。不是最好的解决方案,但它适用于这个实现。我将不胜感激任何解释为什么会发生这种情况的 cmets / 答案。

    /**
    Delegate methods of NSFetchedResultsController to respond to additions, removals and so  on.
    */
    
    - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    
    if (!reordering) {
    
    // The fetch controller is about to start sending change notifications, so prepare the table view for updates.
    NSLog(@"controllerWillChangeContent:");
    
    [self.tableView beginUpdates];
    
    }
    
    }
    
    
    - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
    if (!reordering) {
    
        NSLog(@"didChangeObject:");
        UITableView *tableView = self.tableView;
    
        switch(type) {
            case NSFetchedResultsChangeInsert:
                NSLog(@"ResultsChangeInsert:");
                [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
                break;
    
            case NSFetchedResultsChangeDelete:
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                break;
    
            /*case NSFetchedResultsChangeMove:
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
                break;*/
        }
    
    }
    }
    
    - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
    if (!reordering) {
    
        NSLog(@"didChangeSelection:");
        switch(type) {
            case NSFetchedResultsChangeInsert:
                [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
                break;
    
            case NSFetchedResultsChangeDelete:
                [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
                break;
        }
    }
    }
    
    
    - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    if (!reordering) {
    
        NSLog(@"didChangeContent:");
        // The fetch controller has sent all current change notifications, so tell the table view to process all updates.
        [self.tableView endUpdates];
    }else {
        reordering = NO;
    }
    
    }
    

    【讨论】:

    • @glenstory,上午大部分时间都在寻找解决此问题的方法。感谢您发布此信息。也解决了我的问题!
    • 是的,对我也有用,尤其是因为我的表始终处于编辑模式。
    【解决方案2】:

    我遇到了同样的问题,我通过将保存代码移动到“保存”位置来解决它。即:

    - (void)setEditing:(BOOL)editing animated:(BOOL)animated
    {
        [super setEditing:editing animated:animated];
        if (!editing) {
            //save here
        }
    }
    

    我认为如果你在移动东西的过程中保存东西会搞砸,这是可以理解的。而-(void)setEditing 似乎是个省钱的好地方。

    顺便说一句,感谢您指出问题的原因!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-26
      相关资源
      最近更新 更多