奇怪的重新排序细胞动画/故障

时间:2014-05-11 23:49:30

标签: ios core-data uitableview nsfetchedresultscontroller

我有一个奇怪的动画错误,我认为这是基于我的“moveUp”和“moveDown”方法。我的编辑代码基于THIS GITHUB PROJECT,因此这是方法的起源。

编辑很成功,然而,经过一段时间的游戏,它开始变得非常奇怪。单元格被复制,一些单元格在按下完成按钮后仍然处于编辑模式,即使其他单元格发生变化,然后非常奇怪的是如果您尝试移动仍在编辑中的单元格,它们只是悬停在不可编辑的单元格上。这一切都非常令人困惑。我不知道这是我的moveRowAtIndexPath方法中的问题还是我自定义方法中实际移动的设置。我在下面列举了一个例子。 (对不起,应用程序的背景是白色的,所以看起来代码被推下了。)

enter image description here

UITableViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    NSError *error = nil;

    if (![[self fetchedResultsController]performFetch:&error]) {
        NSLog(@"Error! %@", error);
        abort();
    }
}

- (IBAction)unwindToMainViewController:(UIStoryboardSegue *)unwindSegue {

}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (NSUInteger)countEvents
{
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
    NSFetchRequest *request = [[NSFetchRequest alloc] init] ;
    [request setEntity: entity];
    NSError *error = nil;
    NSUInteger count = [context countForFetchRequest:request error:&error];
    if (count == NSNotFound) {
        count = 0;
    }
    return count;
}

- (void)insertNewObject:(id)sender
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Top" inManagedObjectContext:context];

    [fetchRequest setEntity:entity];

    NSUInteger orderIdx = 1 + [self countEvents];

    NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Top" inManagedObjectContext:context];

    // If appropriate, configure the new managed object.
    // Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
    [newManagedObject setValue:[NSNumber numberWithInteger:orderIdx] forKey:@"orderIdx"];

    // Save the context.
    NSError *error = nil;
    if (![context save:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
}

-(NSManagedObjectContext*)managedObjectContext {

    return [(TTAppDelegate*)[[UIApplication sharedApplication]delegate]managedObjectContext];

}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell*)sender {

    if ([[segue identifier]isEqualToString:@"addTop"]) {

        UINavigationController *navigationController = segue.destinationViewController;

        AddTopViewController *addTopViewController = (AddTopViewController*) navigationController.topViewController;

        Top *addTop = [NSEntityDescription insertNewObjectForEntityForName:@"Top" inManagedObjectContext:self.managedObjectContext];

        addTopViewController.addTop = addTop;

    }

    if ([[segue identifier]isEqualToString:@"showList"]) {
        TopDetailTableViewController *topDetailTableViewController = [segue destinationViewController];

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        Top *selectedTop = (Top *)[self.fetchedResultsController objectAtIndexPath:indexPath];

        topDetailTableViewController.selectedTop = selectedTop;

        UITableViewCell *selectedCell = (UITableViewCell *)sender;
        topDetailTableViewController.title = selectedCell.textLabel.text;
    }

}


-(void) viewWillAppear:(BOOL)animated {

    [self.tableView reloadData];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
    return [sectionInfo numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    // Configure the cell...

    Top *top = [self.fetchedResultsController objectAtIndexPath:indexPath];

    if (!cell) {
        cell = [[UITableViewCell alloc] init];
        cell.showsReorderControl = YES;
    }

    cell.textLabel.text = top.topName;
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];

        NSError *error = nil;
        if (![context save:&error]) {
            // Replace this implementation with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)adjustOrderIdxForRow:(NSInteger)row inSection:(NSInteger)section by:(NSInteger)adjustment
{
    NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:section];
    Top *top = [[self fetchedResultsController] objectAtIndexPath:path];
    top.orderIdx = [NSNumber numberWithInteger:[top.orderIdx integerValue] + adjustment];
}

- (void)moveUp:(NSIndexPath*)fromIndexPath to:(NSIndexPath*)toIndexPath
{
    NSInteger section = fromIndexPath.section;

    Top *objectMoved = [[self fetchedResultsController] objectAtIndexPath:fromIndexPath];
    Top *firstObject = [[self fetchedResultsController] objectAtIndexPath:toIndexPath];
    NSNumber *firstOrderIdx = firstObject.orderIdx;
    for (NSInteger i = toIndexPath.row; i <= fromIndexPath.row - 1; i++) {
        [self adjustOrderIdxForRow:i inSection:section by:-1];
    }
    objectMoved.orderIdx = firstOrderIdx;
}

- (void)moveDown:(NSIndexPath*)fromIndexPath to:(NSIndexPath*)toIndexPath
{
    NSInteger section = fromIndexPath.section;

    Top *objectMoved = [[self fetchedResultsController] objectAtIndexPath:fromIndexPath];
    Top *lastObject = [[self fetchedResultsController] objectAtIndexPath:toIndexPath];
    NSNumber *lastOrderIdx = lastObject.orderIdx;
    for (NSInteger i = fromIndexPath.row + 1; i <= toIndexPath.row; i++) {
        [self adjustOrderIdxForRow:i inSection:section by:1];
    }
    objectMoved.orderIdx = lastOrderIdx;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
    BOOL moveDown = toIndexPath.row > fromIndexPath.row;
    moveDown ? [self moveDown:fromIndexPath to:toIndexPath] : [self moveUp:fromIndexPath to:toIndexPath];
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    NSError *error = nil;
    if (![context save:&error]) {
        NSLog(@"Unresolved error when swapping objects order: %@, %@", error, [error userInfo]);
        abort();
    }
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    Top *event = [self.fetchedResultsController objectAtIndexPath:indexPath];
    NSLog(@"Path: %@, orderIdx: %@", indexPath, event.orderIdx);
}

#pragma mark - Fetched results controller

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Top" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"orderIdx" ascending:NO];
    NSArray *sortDescriptors = @[sortDescriptor];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView beginUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
    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)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:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

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

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

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

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView endUpdates];
}

2 个答案:

答案 0 :(得分:0)

tableView:cellForRowAtIndexPath:中添加以下代码行

cell.clipsToBounds = YES;

此外,当您开始拖动单元格时,请执行此操作

[self.tableView bringSubViewToFront:cell];

在拖动单元格时避免对表格视图进行任何更改

[self.tableView beginUpdated];

// Drag and Drop Cell

// Update data set as needed

[self.tableView endUpdates];

答案 1 :(得分:0)

我设法通过简单地添加

来获得我想要的结果
[self.tableView reloadData];

到我的自定义adjustOrderIdxForRow方法的结尾,以及再次在

-(void)controllerDidChangeContent

方法。说实话,我不知道这是否合适,或者由于这个原因我可能会面对什么类型的反响。但是,该应用程序现在表现得恰当。以下是两种方法。任何批评都是受欢迎的,我再也不知道这是合适的,只是给了我想要的结果。

- (void)adjustOrderIdxForRow:(NSInteger)row inSection:(NSInteger)section by:(NSInteger)adjustment
{
    NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:section];

    Top *top = [[self fetchedResultsController] objectAtIndexPath:path];

    top.orderIdx = [NSNumber numberWithInteger:[top.orderIdx integerValue] + adjustment];

    [self.tableView reloadData];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView endUpdates];

    [self.tableView reloadData];
}