【发布时间】:2019-03-20 15:32:56
【问题描述】:
我收到来自服务器的数据,当他们来找我时,表格已经更新,如何确保当数据到达时表格会爬到底部?
tableView.scrollToBottom(animated: true)
不工作
【问题讨论】:
-
在调用 tableView.scrollToBottom(animated: true) 之前是否尝试过重新加载 tableView
我收到来自服务器的数据,当他们来找我时,表格已经更新,如何确保当数据到达时表格会爬到底部?
tableView.scrollToBottom(animated: true)
不工作
【问题讨论】:
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)
}
}
【讨论】:
您可以使用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)
}
【讨论】:
reloadData() 方法)或插入单元格(调用insertRows(at:with:) 方法)。scrollToRow(at:at:animated:) 方法)。【讨论】: