How is it possible to automatically scroll to the bottom of a UIViewController once the page is opened?
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:
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)
}
}
}
To scroll to the bottom of the table view items, use ScrollToRow. Three parameters are required for this: NSIndexPath: Specify which row item to scroll to. UITableViewScrollPosition: An enumeration of predefined scroll positions.
Smooth Scrolling:- As a result, it affects the smoother scrolling experience. In UITableVIew, Scrolling will be smooth through cell reuse with the help of prepareForReuse() method.
let indexPath = IndexPath(item: noOfRows, section: 0)
yourTableView.scrollToRow(at: indexPath, at: UITableView.ScrollPosition.bottom, animated: true)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With