简体   繁体   English

使用核心数据为UITableViewController添加行动画

[英]Animate row addition to UITableViewController with Core Data

I am working on a Core Data iPad app that uses the Master Detail design pattern. 我正在使用Master Detail设计模式的Core Data iPad应用程序上工作。 I am trying to animate adding a row after updating the managedObjectContext . 我正在尝试在更新managedObjectContext之后添加一行动画。 Currently when I add an object it just appears in the table view. 当前,当我添加一个对象时,它仅出现在表格视图中。 I am using the Stanford Core Data Table View class. 我正在使用Stanford Core Data Table View类。

This is my add object delegate's save method: 这是我的添加对象委托的save方法:

 - (void)addObjectSaveButtonTapped:(iPadAddObjectViewController *)controller {

    [self dismissModalViewControllerAnimated:YES];
    [self setupFetchedResultsController];
}

This is my setupFetchedResultsController method: 这是我的setupFetchedResultsController方法:

    - (void)setupFetchedResultsController {


        NSString *entityName = @"Object";

        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];

        request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"objectName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]];

        self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];


        [self performFetch];

    }

This is performFetch method. 这是performFetch方法。

- (void)performFetch
{
    _debug = YES;

    if (self.fetchedResultsController) {
        if (self.fetchedResultsController.fetchRequest.predicate) {
            if (self.debug) NSLog(@"[%@ %@] fetching %@ with predicate: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), self.fetchedResultsController.fetchRequest.entityName, self.fetchedResultsController.fetchRequest.predicate);
        } else {
            if (self.debug) NSLog(@"[%@ %@] fetching all %@ (i.e., no predicate)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), self.fetchedResultsController.fetchRequest.entityName);
        }
        NSError *error;
        [self.fetchedResultsController performFetch:&error];
        if (error) NSLog(@"[%@ %@] %@ (%@)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), [error localizedDescription], [error localizedFailureReason]);
    } else {
        if (self.debug) NSLog(@"[%@ %@] no NSFetchedResultsController (yet?)", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
    }
    [self.tableView reloadData];
}

Is there any way to add animations to object additions? 有什么方法可以将动画添加到对象添加中?

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:yourIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

UITableView also has a similar method, insertSections:withRowAnimation: UITableView也有一个类似的方法insertSections:withRowAnimation:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM