2015-04-26 71 views
0

我有一个UITableView,它由NSFetchedResultsController备份。我想滚动到的tableView的隐藏行作为被插入表后的搜索栏上方,即用于UITableView的NSFetchedResultsController在插入更改时滚动到顶部

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

知道如果行已交锋的唯一方法是看NSFetchedResultsControllerDelegate API controller:didChangeObject:atIndexPath:forChangeType:newIndexPath

回答

2

我会使用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; 
    } 
} 
+0

这是很酷。我唯一的问题是在不同的代码块中有CATransaction块。我可能在别处也有其他CATransition块。 – p0lAris

+0

@ p0lAris你可以尝试在CATransaction中包装endUpdates。在调用endUpdates之前,我认为表视图不会执行任何操作。此外,CAT交易可以嵌套我认为,所以即使你有其他交易,你应该是安全的。 – Andy

相关问题