Ios 用于UITableView的NSFetchedResultsController在插入更改时滚动到顶部
我有一个UITableView,它由Ios 用于UITableView的NSFetchedResultsController在插入更改时滚动到顶部,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个UITableView,它由NSFetchedResultsController备份。我希望滚动到tableView的顶部,在表中插入一行后隐藏搜索栏,即 -> row created -> row inserted -> row insert animation finished -> **scroll to top hiding search controller** 知道行是否已添加的唯一方法是查看NSFetchedResultsControllerDel
NSFetchedResultsController
备份。我希望滚动到tableView的顶部,在表中插入一行后隐藏搜索栏,即
-> row created
-> row inserted
-> row insert animation finished
-> **scroll to top hiding search controller**
知道行是否已添加的唯一方法是查看
NSFetchedResultsControllerDelegate
APIcontroller:didChangeObject:atIndexPath:forChangeType:newIndexPath
当UITableView
完成动画时,我会使用CATTransaction
获得通知:
- (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;
}
}
当
UITableView
完成动画时,我会使用CATransaction
获得通知:
- (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;
}
}
那很酷。我唯一的问题是在不同的代码块中有CATTransaction块。我可能在其他地方也有其他CATTransaction块。@p0lAris您可以尝试在CATTransaction中包装endUpdates。我认为在调用endUpdates之前,表视图不会做任何事情。我认为CATTransaction也可以嵌套,所以即使有其他事务,也应该是安全的。这很酷。我唯一的问题是在不同的代码块中有CATTransaction块。我可能在其他地方也有其他CATTransaction块。@p0lAris您可以尝试在CATTransaction中包装endUpdates。我认为在调用endUpdates之前,表视图不会做任何事情。我认为CATTransaction也可以嵌套,所以即使有其他事务,您也应该是安全的。