0Day Forums
scrollToRowAtIndexPath with UITableView does not work - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: Swift (https://zeroday.vip/Forum-Swift)
+--- Thread: scrollToRowAtIndexPath with UITableView does not work (/Thread-scrollToRowAtIndexPath-with-UITableView-does-not-work)

Pages: 1 2


scrollToRowAtIndexPath with UITableView does not work - hinesj - 07-19-2023

I have in iOS8 a table view like this:

tableView = UITableView(frame: view.bounds, style: .Plain)
view.addSubview(tableView)

When the user types and sends some text in the keyboard, the application ivoke the following method to scroll the tableView. The goal is to view the new text in the screen (like a chat)


let numberOfRows = tableView.numberOfRowsInSection(0)
if numberOfRows > 0 {
let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: 0)
tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: animated)
}

But the table view does not scroll to the bootom.

Someone has a solution?

Thank you.

Explanation:

Definition of the class:

class ChatViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextViewDelegate

then I have the definition of the table view:

var tableView: UITableView!

in viewDidLoad:

tableView = UITableView(frame: view.bounds, style: .Plain)
tableView.dataSource = self
tableView.delegate = self
view.addSubview(tableView)

then I have the call to the code that should make the scroll (second block of code on my answer).
When I launch the application I expect the tableview to scroll down, but it does not work.



RE: scrollToRowAtIndexPath with UITableView does not work - Mrjoannm - 07-19-2023

**Swift 5 Version**

extension UITableView {
func tableViewScrollToBottom(animated: Bool) {
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(300)) {
let numberOfSections = self.numberOfSections
let index = self.numberOfRows(inSection: 0)
if index > 0 {
let indexPath = IndexPath(row: index-1, section: 0)
self.scrollToRow(at: indexPath, at: UITableView.ScrollPosition.bottom, animated: animated)
}
}
}
}

**You can use like:**

[YOUR_TABLE_VIEW].tableViewScrollToBottom(animated: [TRUE])



RE: scrollToRowAtIndexPath with UITableView does not work - addable238326 - 07-19-2023

I go the same problem in late 2020.

Since i really did not want some magic dispatch-timeout, i played around a bit. What helped (and was not mentioned here before) was a forced layout for the UITableView:

_viewTable.layoutIfNeeded()
_viewTable.scrollToRow(at:IndexPath(row:index, section:0), at:.middle, animated:false)


RE: scrollToRowAtIndexPath with UITableView does not work - phacoanaphylaxis914655 - 07-19-2023

Try below:

```
if tableView.numberOfRows(inSection: 0) > 0 {
tableView.scrollToRow(at: IndexPath(row: ROW, section: SECTION), at: .top, animated: false)
}
```


RE: scrollToRowAtIndexPath with UITableView does not work - octoroons812669 - 07-19-2023

I solved the issue by calling `scrollToRow()` in the `viewWillLayoutSubviews()


RE: scrollToRowAtIndexPath with UITableView does not work - tuft351 - 07-19-2023

I know this is an older thread, but it's still relevant. Instead of dealing with timeouts that could sometimes fail to be well timed, use synchronous blocks on the main thread (normally not something I'd do, but we're talking milliseconds):

DispatchQueue.main.sync {
// Reload code goes here...
tableView.reloadData()
}

DispatchQueue.main.sync {
// Row, section and other checks / initialization go here...
tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}

This always works for me.





RE: scrollToRowAtIndexPath with UITableView does not work - uninjuriousness991572 - 07-19-2023

It's simple. Just do the scroll operation in **viewDidLayoutSubViews()**

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.scrollToBottom(animated: false)
}

fileprivate func scrollToBottom(animated: Bool) {
if self.items.count > 0 {
let lastIndex = IndexPath(row: items.count - 1, section: 1)
self.tableView.scrollToRow(at: lastIndex, at: .bottom, animated: animated)
}
}
It seems like tableview could get the correct positon after layout subviews.


RE: scrollToRowAtIndexPath with UITableView does not work - Drkalinin10 - 07-19-2023

// First figure out how many sections there are
let lastSectionIndex = self.tblTableView!.numberOfSections() - 1

// Then grab the number of rows in the last section

let lastRowIndex =
self.tblTableView!.numberOfRowsInSection(lastSectionIndex) - 1

// Now just construct the index path

let pathToLastRow = NSIndexPath(forRow: lastRowIndex, inSection: lastSectionIndex)

// Make the last row visible
self.tblTableView?.scrollToRowAtIndexPath(pathToLastRow, atScrollPosition: UITableViewScrollPosition.None, animated: true)


RE: scrollToRowAtIndexPath with UITableView does not work - lavernaixxfykytrr - 07-19-2023

All above codes are fine, but in my case when I want to load the `tableView` as well as scroll to bottom of the `tableView` **instantly** when the **controller loads**, for that situation above methods are taking some time to scroll to the bottom of the `tableView`. So I have followed these codes.

Override `tableView` reload as

extension UITableView
{
func reloadData(completion: @escaping ()->()) {
UIView.animate(withDuration: 0, animations: { self.reloadData() })
{ _ in completion() }
}
}

Written scroll to bottom method as follows:

func tableViewScrollToBottom
{
if self.tblChat.contentSize.height > self.tblChat.frame.size.height
{
let offset:CGPoint = CGPoint(x: 0,y :self.tblChat.contentSize.height-self.tblChat.frame.size.height)
self.tblChat.setContentOffset(offset, animated: false)
}
}

and then reload `tableView` when data loads completes from web service or from any core data entity:

tblChat.reloadData()
{
self.tableViewScrollToBottom
}

Now its working smooth as I wanted.


RE: scrollToRowAtIndexPath with UITableView does not work - amber626031 - 07-19-2023

The solution below worked for me. It's a combination of solutions found on StackOverflow. I called "`scrollToRowAtIndexPath`" after a very short delay.



func tableViewScrollToBottom(animated: Bool) {

let delay = 0.1 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

dispatch_after(time, dispatch_get_main_queue(), {

let numberOfSections = self.tableView.numberOfSections()
let numberOfRows = self.tableView.numberOfRowsInSection(numberOfSections-1)

if numberOfRows > 0 {
let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1))
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: animated)
}

})
}


called by :

tableViewScrollToBottom(true)

**Swift 3**

func tableViewScrollToBottom(animated: Bool) {
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(300)) {
let numberOfSections = self.tableView.numberOfSections
let numberOfRows = self.tableView.numberOfRows(inSection: numberOfSections-1)

if numberOfRows > 0 {
let indexPath = IndexPath(row: numberOfRows-1, section: (numberOfSections-1))
self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: animated)
}
}
}