ios - 加载 View Controller 时如何滚动到特定的 UITableViewCell 顶部到 UITableView 顶部

标签 ios swift uitableview uiscrollview

我想在加载 View Controller 时将特定的表格 View 单元格滚动到顶部

这个方法运行良好。但是当单元格高度动态变化时,单元格不会滚动到顶部,而是滚动到中心

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "slideshowTableViewCell", for: indexPath) as! slideshowTableViewCell

    if(self.scrollEnabled){

        let indexPath = NSIndexPath(row: self.imageId, section: 0)
        self.slideshowView.scrollToRow(at: indexPath as IndexPath, at: .top, animated: true)


        self.scrollEnabled = false
    }

最佳答案

您应该在 viewWillAppear 上调用 scrollToRow

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    tblList.scrollToRow(at: Your_desired_indexPath, at: .top, animated: true)
}

关于ios - 加载 View Controller 时如何滚动到特定的 UITableViewCell 顶部到 UITableView 顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50384833/

相关文章:

ios - 如何使用自定义类的 TableView 单元格的本地化?

ios - 如何在组合中安排异步调用的同步序列?

ios - 如何从 CoreLocations geocodeAddressString 获取更多结果?

swift - 等到异步 api 调用完成 - Swift/IOS

ios - 拖动时更新 MKPointAnnotation 对象

iphone - 如何在 UITableViewCell 中动态调整两个文本标签的宽度?

ios - 使用 NSDictionary 填充 UITableView,其中包含来自 mysql db 的已解析 JSON 数据

ios - 有没有办法检测 TableView 中索引部分的选择?

ios - 使用 willDisplay cell 方法向上滚动 tableView 时,如何从 API 获取数据并更新单元格和部分?

iphone - 将同名的注释分组