【问题标题】:Grouped UITableView scrollToRow not working分组的 UITableView scrollToRow 不起作用
【发布时间】:2021-07-17 15:15:19
【问题描述】:

我正在使用UITableView(frame: .zero, style: .grouped) 与多个部分和单元格的动态高度。每个部分都有一个标题。

我正在尝试滚动到表格视图的底部

let lastSection = tableView.numberOfSections - 1
let lastIndexPath = IndexPath(row: tableView.numberOfRows(inSection: lastSection) - 1, section: lastSection)
tableView.scrollToRow(at: lastIndexPath, at: .bottom, animated: true)

我的问题是:

部分中只有一项时,表格视图将滚动到标题而不是该项。当有多个项目时,一切正常。

另外对于plain风格的table view,一切正常。

【问题讨论】:

    标签: ios swift uitableview uikit


    【解决方案1】:

    尝试使用下面的代码,

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { 
        let lastSection = tableView.numberOfSections - 1
        let lastIndexPath = IndexPath(row: tableView.numberOfRows(inSection: lastSection) - 1, section: lastSection)
        tableView.scrollToRow(at: lastIndexPath, at: .bottom, animated: true)
    }
    

    【讨论】:

    • 不幸的是,它没有帮助。
    【解决方案2】:

    事实证明estimatedHeightForHeaderInSection 不够精确。即使我不使用节标题的动态高度。

    所以最后的解决办法是:在tableView(_ tableView: UITableView, heightForHeaderInSection section: Int)tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) 中使用相同的值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-14
      相关资源
      最近更新 更多