Swift 仅在新添加的单元格中重新加载TableView
我有一个scrollToBottom函数,通知应用程序何时开始批量抓取内容、图片和检查Swift 仅在新添加的单元格中重新加载TableView,swift,uitableview,reload,batch-fetching,Swift,Uitableview,Reload,Batch Fetching,我有一个scrollToBottom函数,通知应用程序何时开始批量抓取内容、图片和检查 //1: User Scrolls all the way down calls beginBatchFetch() func scrollViewDidScroll(_ scrollView: UIScrollView) { let offsetY = scrollView.contentOffset.y let contentHeight = scrollV
//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()
}
}
}
}
这些是BeginBatchFetch方法。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。我只想重新加载新添加的单元格。例如,如果我在batchfetch之前有10个单元格,然后在batchfetch之后有20个单元格,我只想为那些单元格11-20重新加载tableview
我在网上查看,StackOverFlow让我想到:
据我所知,这只会重新加载显示的单元格。但是,我的批处理回迁可能不会返回更多的值。因此,我的tableview将显示与批处理获取之前相同的单元格,这将重新加载这些单元格。由于这些单元格以前已加载,我不想浪费数据/破坏我的tableview
我该怎么办,谢谢?您需要做的是计算需要重新加载的行,并确保在使用新数据更新数据源后,将这些索引路径传递到“重新加载行”函数中
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) }
}
private func calculateInExpathStoreLoad(来自newItems:[Items])->[IndexPath]{
让startIndex=results!.count-newItems.count
让endIndex=startIndex+newItems.count
return(startIndex..这是我添加的补充:
我可能把for-in-loop函数搞砸了。我还不太懂映射
func beginContentBatchFetch() {
contentFetchMore = true
ProgressHUD.show()
let oldcount = contentObjectIdArray.count
var IndexPathsToReload = [IndexPath]()
var startIndex = Int()
var endIndex = Int()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.checkQueryContinous()
let newElements = self.contentObjectIdArray.count - oldcount
startIndex = self.contentObjectIdArray.count - newElements
endIndex = self.contentObjectIdArray.count
for index in startIndex..<endIndex {
let indexPath = IndexPath(row: index, section: 0)
IndexPathsToReload.append(indexPath)
}
if newElements > 0 {
self.BarSelectedTableView.reloadRows(at: IndexPathsToReload, with: .fade)
}
ProgressHUD.dismiss()
}
}
func beginContentBatchFetch(){
contentFetchMore=true
ProgressHUD.show()
让oldcount=contentObjectiveDarray.count
var IndexPathsToReload=[IndexPath]()
var startIndex=Int()
var endIndex=Int()
DispatchQueue.main.asyncAfter(截止日期:.now()+1.0){
self.checkQueryContinuous()
让新元素=self.contentObjectDarray.count-oldcount
startIndex=self.ContentObjectiveDarray.count-新元素
endIndex=self.contentObjectiveDarray.count
对于startIndex..0中的索引{
self.BarSelectedTableView.reloadRows(位于:IndexPathsToReload,带:.fade)
}
ProgressHUD.discover(解雇)
}
}
我正在尝试理解您的函数。我有一个arrayA,它可以容纳10个元素。我运行批处理获取并添加10个元素。arrayA有20个元素。然后我使用arrayA.count-new items.count来获取开始索引。然后您希望我使用startIndex+newItems.count,但我认为我可以使用arrayA.count-1作为结束索引索引。然后你想让我映射通过以返回一个结果。我真的不明白结果。我如何将这个结果实现到reloadRowsAtIndexPath函数中?如果我得到零个新元素呢?如果我得到零个新元素,startIndex将是10,但我的数组中没有第10个索引元素,所以我的应用程序将崩溃。我想说的是什么这意味着您不需要使用该方法重新加载整个表视图,只需重新加载新的行,并且只有在有任何新行需要重新加载时才需要。您的旧行应该可以。
func beginContentBatchFetch() {
contentFetchMore = true
ProgressHUD.show()
let oldcount = contentObjectIdArray.count
var IndexPathsToReload = [IndexPath]()
var startIndex = Int()
var endIndex = Int()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.checkQueryContinous()
let newElements = self.contentObjectIdArray.count - oldcount
startIndex = self.contentObjectIdArray.count - newElements
endIndex = self.contentObjectIdArray.count
for index in startIndex..<endIndex {
let indexPath = IndexPath(row: index, section: 0)
IndexPathsToReload.append(indexPath)
}
if newElements > 0 {
self.BarSelectedTableView.reloadRows(at: IndexPathsToReload, with: .fade)
}
ProgressHUD.dismiss()
}
}