【问题标题】:How to scroll to a specific UITableViewCell top to UITableView top when load view controller加载视图控制器时如何滚动到特定的 UITableViewCell 顶部到 UITableView 顶部
【发布时间】:2018-05-17 06:35:47
【问题描述】:

我想在加载视图控制器时将特定的表格视图单元格滚动到顶部

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

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
    }

【问题讨论】:

  • 请停止在 cellForRowAt 方法上调用 scrollToRow。这是非常糟糕的做法。您可以计算 indexPath 并在 viewWillAppear 上调用 scrollToRow 并使其仅调用一次。

标签: ios swift uitableview uiscrollview


【解决方案1】:

您应该在viewWillAppear 上致电scrollToRow

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 2010-10-18
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    相关资源
    最近更新 更多