繁体   English   中英

自动滚动到 UITableView 底部

[英]Auto scroll to bottom of UITableView

打开页面后,如何自动滚动到 UIViewController 的底部?

目前,当我打开页面时,它一直滚动到顶部。但我需要它在底部,因为这是最新消息所在的位置

这是表格视图的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)

这在 Swift 4 中对我有用

我已经覆盖了 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)
}

感谢这篇文章一直向下滚动到 UITableView 的底部以获得答案,请注意这个问题并不完全相同,它们向下滚动 reloadData 而不是出现的屏幕。

暂无
暂无

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

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