问题
I've noticed a weird UITableView behaviour which only seems to occur on iOS 11 devices.
Right after inserting a new row (changing data source and then calling reloadData, UITableView
won't scroll to that row when calling scrollToRow
or scrollToBottom()
method.
When doing the some thing on iOS 10 or earlier version, it works perfectly and UITable scrolls like it's supposed to.
Thanks!
回答1:
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 small delay is working in my case.
回答2:
Not sure if the situation I encountered applies to this question, but I also had a situation where UITableView.scrollTo...
started misbehaving from iOS 11.
In my case, I was calling UITableView.reload...
and UITableView.scrollTo...
consecutively. And what it turned out is that the order matters.
This works :
tableView.reloadData()
tableView.scrollToRow(at: indexPath, at: .top, animated: true)
This doesn't :
tableView.scrollToRow(at: indexPath, at: .top, animated: true)
tableView.reloadData()
Hopefully this helps.
回答3:
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)
}
回答4:
Just DispatchQueue.main.async
works for me.
来源:https://stackoverflow.com/questions/46075517/uitableview-scrolltorow-no-longer-works-on-ios-11-right-after-adding-a-new-row