-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using insertRowsAtIndexPaths instead of reloadData causes issues with contentOffset #31
Comments
Hi, Thanks for trying to understand how it works. I put lots of efforts in making it work with batch updates in this branch: https://github.com/pronebird/UIScrollView-InfiniteScroll/tree/fix-dynamic-tableview-updates I have tried to workaround the timing issue by using However for this to work developer would have to use custom I'll play around with |
Here's my full code if it helps: /**
Set up the infinite scroll handler to use the `viewModel`'s paginator.
*/
func setupInfiniteScroll() {
searchTableView.infiniteScrollIndicatorStyle = .White
searchTableView.addInfiniteScrollWithHandler() { scrollView in
let startIndexPaths = self.allIndexPaths()
let startSections = self.viewModel.search.dates.count
self.viewModel.nextPage() {
let endIndexPaths = self.allIndexPaths()
let endSections = self.viewModel.search.dates.count
let newIndexPaths = Array(endIndexPaths.subtract(startIndexPaths))
let newSections = NSIndexSet(indexesInRange: NSRange(startSections..<endSections))
// Use insertSections and insertRowsAtIndexPaths so we don't reload/reanimate
// the existing cells.
self.searchTableView.beginUpdates()
self.searchTableView.insertSections(newSections, withRowAnimation: .None)
self.searchTableView.insertRowsAtIndexPaths(newIndexPaths, withRowAnimation: .None)
self.searchTableView.endUpdates()
// Force the table view to update its contentSize; if we don't do this,
// finishInfiniteScroll() will adjust contentInsets and cause contentOffset
// to be off by an amount equal to the height of the activity indicator.
// See https://github.com/pronebird/UIScrollView-InfiniteScroll/issues/31
self.searchTableView.contentSize = self.searchTableView.sizeThatFits(CGSize(width: self.searchTableView.width, height: CGFloat.max))
self.searchTableView.finishInfiniteScroll()
}
}
} |
Sorry it takes that long to get any feedback from me, I may be able to spare an hour or two to take a look at this issue over weekend. I really really appreciate your efforts on discovering the potential fix for dynamic updates in table views. |
Do you mind testing the proposed fix with your app? https://github.com/pronebird/UIScrollView-InfiniteScroll/tree/fix/dynamic-tableview-updates |
I had turned off github notifications by accident -- will check this out tonight! Sorry for delay. |
I just realized that it's been a while and I entirely forgot to merge this. Will do now as it seems to work on iOS 9.3 for both self-sizing and fixed height cells. |
Released with 0.9.0. It has issues with self-sizing cells while table view is decelerating or user is messing around with scroll – animations look really chaotic. |
If you are updating your tableView using
insertRowsAtIndexPaths
rather thanreloadData
, the tableView doesn't calculate its contentSize before the infinite scroll handler adjusts thecontentInset
to remove the activity indicator. This causes thecontentOffset
property to be incorrectly adjusted upward by an amount equal to the height of the activity indicator.I got around it in my own code by forcing the table view to update its
contentSize
after inserting rows:I'm not sure if there is a way to detect if the tableView needs to update its
contentSize
; if there is, it might be worth putting this logic inside a conditional inpb_stopAnimatingInfiniteScrollWithCompletion
The text was updated successfully, but these errors were encountered: