简体   繁体   English

自动滚动到 UITableView 底部

[英]Auto scroll to bottom of UITableView

How is it possible to automatically scroll to the bottom of a UIViewController once the page is opened?打开页面后,如何自动滚动到 UIViewController 的底部?

Currently when I open the page its scrolled all way to the top.. But I need it at the bottom as that is where the latest messages are目前,当我打开页面时,它一直滚动到顶部。但我需要它在底部,因为这是最新消息所在的位置

Heres the swift for the table view:这是表格视图的swift:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.dataArr.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "messages") as! UITableViewCell        
    cell.selectionStyle = .none
    let message = cell.viewWithTag(100) as! UILabel
    let name = cell.viewWithTag(101) as! UILabel
    let data = self.dataArr[indexPath.row] as! [String:AnyObject]
    message.text = " " + String(describing: data["message"]!) + " "
    name.text = String(describing: data["name"]!)

    return cell
}


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    var mapStr = ""
    let data = self.dataArr[indexPath.row] as! [String:AnyObject]
    let message = String(describing: data["message"]!)
    let array = message.components(separatedBy: "\n") as! [String]
    let arrayLoc = message.components(separatedBy: "Location: ") as! [String]
        if arrayLoc.count > 0 {
            mapStr = arrayLoc[1]
        }
    print(mapStr)
    if mapStr.count > 0 {
        self.open(scheme: mapStr)
    }

}

}
let indexPath = IndexPath(item: noOfRows, section: 0)
yourTableView.scrollToRow(at: indexPath, at: UITableView.ScrollPosition.bottom, animated: true)

This worked for me in Swift 4这在 Swift 4 中对我有用

I have overidden the viewDidAppear function of my UITableViewController class.我已经覆盖了 UITableViewController 类的 viewDidAppear 函数。

override func viewDidAppear(_ animated: Bool) {
    let scrollPoint = CGPoint(x: 0, y: self.tableView.contentSize.height - self.tableView.frame.size.height)
    self.tableView.setContentOffset(scrollPoint, animated: false)
}

Thanks to this post Scroll all the way down to the bottom of UITableView for the answer, note that question's not quite the same, they scroll down on reloadData rather than the screen appearing.感谢这篇文章一直向下滚动到 UITableView 的底部以获得答案,请注意这个问题并不完全相同,它们向下滚动 reloadData 而不是出现的屏幕。

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

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