iphone - UITableView 中的 moveRowAtIndexPath 导致动画不正确

标签 iphone cocoa-touch core-data uitableview

我有一个简单的 UITableView Controller ,用于显示 CoreData。我正在尝试实现 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;并在动画方面遇到问题。核心数据存储已更新,但动画不起作用。

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

例如:

初始订单:

alt text

第 2 项之后到顶部:

alt text

或者,初始订单:

alt text

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

alt text

相关代码如下:

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

最佳答案

该问题是由委托(delegate)干扰我的重新排序实现引起的。 我在保存 ManagedObjectContext 之前添加了一个 bool 值:

reordering = YES; [managedObjectContext save:nil];

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

/**
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;
}

}

关于iphone - UITableView 中的 moveRowAtIndexPath 导致动画不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4230353/

相关文章:

iphone - 从配置文件和代码签名中破解 xCode 后没有 NSLog

iphone - iPhone中的自定义音频播放器

ios - NSMutableArray enumerateObjectsUsingBlock 并不像 Apple 所说的那样同步

ios - 有没有办法部署已经预先填充Core Data数据库的iOS应用?

Swift - 从核心数据中检索图像

iphone - 如何从另一个 iOS 应用程序打开一个 iOS 应用程序

ios - 如何创建配置文件

iphone - 我在委托(delegate)函数shouldStartLoadWithRequest中返回NO后是否正常调用didFailLoadWithError?

iphone - 在 iOS 7 中,为什么 View 高度与 iOS 6 中的相同 View 不同

ios - CoreData 的问题