首页 > 解决方案 > .reloadSections 后的 UITableView 偏移量不正确

问题描述

当我点击显示/隐藏内容时,我有下一个代码。

func showHideAssets() {
     isOpenHiddenAssets = !isOpenHiddenAssets

     tableView.beginUpdates()
     tableView.reloadSections(IndexSet(integer: 1), with: .fade)
     tableView.endUpdates()
}

当第一部分包含许多项目时,重新加载部分后,表格滚动到底部且偏移不正确

func numberOfSections(in tableView: UITableView) -> Int {
     return 2
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     if section == 0 {
          return 10
     }
     return isOpenHiddenAssets ? assetsHiddenItems.count : 0
}

第一部分有 3 个项目的视频。效果很好 视频1

第一部分有 10 个项目的视频。它有错误。视频 2

标签: iosswiftuitableview

解决方案


解决方案是更换

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

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

推荐阅读