UITableView scrollToRow no longer works on iOS 11 right after adding a new row

I noticed a strange UITableView behavior that only appears on iOS 11 devices.

Immediately after inserting a new row (changing the data source, and then calling reloadData, the UITableView will not scroll to this row when calling the scrollToRow or scrollToBottom() method.

When you do something on iOS 10 or earlier, it works fine and UITable-scrolls as intended.

Thanks!

+8
ios iphone uitableview
source share
3 answers
 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; }); 

Adding a little delay works in my case.

+3
source share

Accepted answer in Swift 4 syntax

 let deadlineTime = DispatchTime.now() + .seconds(1) DispatchQueue.main.asyncAfter(deadline: deadlineTime) { self.tableView.scrollToRow(at: indexPath, at: .middle, animated: true) } 
+1
source share

Not sure if the situation I came across relates to this issue, but I also had a situation where UITableView.scrollTo... started to behave badly with iOS 11.

In my case, I called UITableView.reload... and UITableView.scrollTo... sequentially. And it turned out that order matters.

It works:

 tableView.reloadData() tableView.scrollToRow(at: indexPath, at: .top, animated: true) 

It does not mean:

 tableView.scrollToRow(at: indexPath, at: .top, animated: true) tableView.reloadData() 

Hope this helps.

0
source share

All Articles