【问题标题】:How to load tableView from the bottom如何从底部加载tableView
【发布时间】:2019-03-20 15:32:56
【问题描述】:

我收到来自服务器的数据,当他们来找我时,表格已经更新,如何确保当数据到达时表格会爬到底部?

tableView.scrollToBottom(animated: true)

不工作

【问题讨论】:

  • 在调用 tableView.scrollToBottom(animated: true) 之前是否尝试过重新加载 tableView

标签: ios swift uikit tableview


【解决方案1】:

tableView没有scrollToBottom方法,我觉得你应该在UITableView的扩展中加入这个方法

示例

import UIKit

extension UITableView {
    func scrollToBottom(animated: Bool) {
        let y = contentSize.height - frame.size.height
        setContentOffset(CGPoint(x: 0, y: (y<0) ? 0 : y), animated: animated)
    }
}

【讨论】:

    【解决方案2】:

    您可以使用tableView.scrollToRow(at:at:animated:)。例如,假设您有一个显示在表格视图中的字符串数组:

    var items = ["1", "2", "3"]
    
    func scrollToBottom() {
        let indexPathToReach = IndexPath(row: items.count - 1, section: 0)
        tableView.scrollToRow(at: indexPathToReach, at: .bottom, animated: true)
    }
    

    【讨论】:

      【解决方案3】:
      1. 收到数据后更新表视图数据源。
      2. 在主线程中重新加载表格视图(调用reloadData() 方法)或插入单元格(调用insertRows(at:with:) 方法)。
      3. 在主线程中滚动到底部(调用scrollToRow(at:at:animated:) 方法)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-13
        • 2012-10-23
        • 2017-10-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多