我有一个scrollToBottom函数,该函数通知应用程序何时开始内容,图片和检查的BeginBatchFetches。
//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()
}
}
}
}
这些是beginBatchFetchMethods。 Self.reload当前会重新加载整个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()
}
}
而不是重新加载整个TableView。我只想重新加载新添加的单元格。例如,如果我在批量提取之前有10个单元格,然后在批量提取之后有20个单元格,那么我只想为11-20个单元格重新加载表格视图。
我看了网上,然后StackOverFlow带我到这里: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.
}
据我了解,这只会重新加载显示的单元格。但是,我的batchfetches可能不会返回更多值。结果,我的tableview将显示与批量提取之前相同的单元格,这将重新加载这些单元格。由于这些单元是先前加载的,因此我不想浪费数据/构建表视图。
我该怎么办,谢谢?
[您需要做的是计算需要重新加载的行,并确保在使用新数据更新数据源之后,将这些indexPath传递给reload rows函数。
private func calculateIndexPathsToReload(from newItems: [Items]) -> [IndexPath] {
let startIndex = results!.count - newItems.count
let endIndex = startIndex + newItems.count
return (startIndex..<endIndex).map { IndexPath(row: $0, section: 0) }
}