gpt4 book ai didi

ios - UITableView 的 NSFetchedResultsController 在插入更改时滚动到顶部

转载 作者:行者123 更新时间:2023-11-28 19:49:52 26 4
gpt4 key购买 nike

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

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

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

最佳答案

我会使用 CATransactionUITableView 完成动画时得到通知:

- (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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com