[英]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.