I have a scrollToBottom function that notifies the app when to beginBatchFetches for content, pics, and checks.
//1: User Scrolls all the way down calls beginBatchFetch()
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offsetY = scrollView.contentOffset.y
let contentHeight = scrollView.contentSize.height
print("offsetY: \(offsetY)")
if offsetY > contentHeight - scrollView.frame.height {
if self.viewSelected == "Content" {
if !contentFetchMore {
beginContentBatchFetch()
}
} else if self.viewSelected == "Pics" {
if !picFetchMore {
beginPicBatchFetch()
}
} else if self.viewSelected == "Checks" {
if !checksFetchMore {
beginChecksBatchFetch()
}
}
}
}
These are the beginBatchFetchMethods. Self.reload currently reloads the whole Tableview:
func beginContentBatchFetch() {
contentFetchMore = true
ProgressHUD.show()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.checkQueryContinous()
ProgressHUD.dismiss()
}
}
func beginPicBatchFetch() {
picFetchMore = true
ProgressHUD.show()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.PicContinous()
self.Reload()
ProgressHUD.dismiss()
}
}
func beginChecksBatchFetch() {
checksFetchMore = true
ProgressHUD.show()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.checkQueryContinous()
self.Reload()
ProgressHUD.dismiss()
}
}
Instead of reloading the whole TableView. I would like to only reload the new added cells. For example, if I had 10 cells before the batchfetch and then 20 cells after the batchfetch, I would like to only reload the tableview for those cells 11-20.
I looked online and StackOverFlow brought me to this: iOS Swift and reloadRowsAtIndexPaths compilation error
var _currentIndexPath:NSIndexPath?
if let indexPath = _currentIndexPath {
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation:
UITableViewRowAnimation.None)
}
else {
` //_currentIndexPath` is nil.
// Error handling if needed.
}
This from what I understand only reloads the cells that are shown. However, my batchfetches may not return more values. As a result, my tableview will display the same cells as before the batch fetch and this would reload those cells. Since those cells were previously loaded, I do not want to waste data/screw up my tableview.
What do I do, thanks?
What you need to do is calculate the rows that need to be reloaded and make sure you pass those indexPaths into the reload rows function after you update your data source with the new data.