简体   繁体   English

如何从底部加载tableView

[英]How to load tableView from the bottom

I receive data from the server and when they come to me the table is updated, how to make sure that when the data came the table would creep down to the bottom? 我从服务器接收数据,当他们来找我时,表格会更新,如何确保数据传输时表格会下降到底部?

tableView.scrollToBottom(animated: true)

don't working 不工作

tableView has no method scrollToBottom, I think you should add this method in the extension of UITableView tableView没有方法scrollToBottom,我认为你应该在UITableView的扩展中添加这个方法

Example : 示例

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)
    }
}

You can use tableView.scrollToRow(at:at:animated:) . 您可以使用tableView.scrollToRow(at:at:animated:) For example, let's assume you have an array of string that are show in the table view: 例如,假设您有一个在表视图中显示的字符串数组:

var items = ["1", "2", "3"]

func scrollToBottom() {
    let indexPathToReach = IndexPath(row: items.count - 1, section: 0)
    tableView.scrollToRow(at: indexPathToReach, at: .bottom, animated: true)
}
  1. Update table view data source after receiving data. 接收数据后更新表视图数据源。
  2. Reload table view (call reloadData() method) or insert cells (call insertRows(at:with:) method) in main thread. 重新加载表视图(调用reloadData()方法)或在主线程中插入单元格(调用insertRows(at:with:)方法)。
  3. Scroll to bottom (call scrollToRow(at:at:animated:) method) in main thread. 在主线程中滚动到底部(调用scrollToRow(at:at:animated:)方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM