自动滚动到UITableView的底部

Oli*_*ris 3 uitableview ios swift

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

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

这是表格视图的快捷方式:

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

}

}
Run Code Online (Sandbox Code Playgroud)

Abh*_*tra 6

let indexPath = NSIndexPath(item: noOfRows, section: 0)
yourTableView.scrollToRow(at: indexPath as IndexPath, at: UITableViewScrollPosition.bottom, animated: true)
Run Code Online (Sandbox Code Playgroud)


小智 5

这在 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)
}
Run Code Online (Sandbox Code Playgroud)

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