ios - 插入新行后如何停止 UITableView 滚动到顶部?

标签 ios uiscrollview tableview catransition

我在我的应用程序中使用了 Accordion View ,当我在 UITableView 中插入新行时,如果该部分位于可见区域,则一切正常。但是当我们更新下部行时,UITableView 会自动滚动到顶部。这里有什么问题?谁能帮我解决这个问题?

   [CATransaction begin];

    [CATransaction setCompletionBlock:^{
     NSLog(@"Completed");
    } 
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths: indexPathsToInsert withRowAnimation: UITableViewRowAnimationAutomatic];
    [self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
    [CATransaction commit];

有什么建议吗?我尝试了 scrollToRowAtIndexPathsetContentOffSet

最佳答案

您是否使用自动布局让表格 View 计算单元格高度?如果是,您必须尽快将 estimatedHeight 设置为 cell 的实际高度。如果不同单元格的高度不同:

func tableView(_ tableView: UITableView, estimatedHeightForRowAt
indexPath: IndexPath) -> CGFloat

“ Accordion 教程”之所以有效,是因为它使用了固定的单元格高度。

关于ios - 插入新行后如何停止 UITableView 滚动到顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37944728/

相关文章:

JavaFx:如何正确触发 TableCell 中的 updateItem

ios - 无法更改 Storyboard 中 ViewController 的方向

swift - 如何继续使图像在 ScrollView 特定区域内滚动?

ios - 为什么我的 iOS App 的 Documents 目录变成了一个文件?

ios - 翻转 NSScrollView 问题

ios - UITapGestureRecognizer 无法在 UIScrollView 中工作

json - Swiftyjson - JSON 到带有字母部分的 TableView

java - TableView 项目未删除但不响应

ios - “显示缩放”iPhone 6/6s 设置模糊图形

ios - SpriteKit 粒子发射器未出现在 Xcode 编辑器中