我有一个由NSFetchedResultsController备份的UITableView。我希望滚动到tableView的顶部,在插入到表中的一行之后隐藏搜索栏,即

-> row created
-> row inserted
-> row insert animation finished
-> **scroll to top hiding search controller**


知道行是否已添加的唯一方法是查看NSFetchedResultsControllerDelegate API controller:didChangeObject:atIndexPath:forChangeType:newIndexPath

最佳答案

CATransaction完成动画时,我会使用UITableView来获得通知:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [CATransaction begin];
    [CATransaction setCompletionBlock:^{
        if(self.shouldScrollToTop) {
            [self.tableView setContentOffset:CGPointZero animated:YES];
        }
        self.shouldScrollToTop = NO;
    }];

    [self.tableView beginUpdates];
}

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

    [CATransaction commit];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertRowsAtIndexPaths:@[ newIndexPath ] withRowAnimation:UITableViewRowAnimationAutomatic];

            self.shouldScrollToTop = YES;
            break;
    }
}

关于ios - UITableView的NSFetchedResultsController在插入更改时滚动到顶部,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29880618/

10-12 00:11
查看更多