gpt4 book ai didi

swift - 仅在新添加的单元格处重新加载 TableView

转载 作者:行者123 更新时间:2023-11-30 10:31:55 25 4
gpt4 key购买 nike

我有一个scrollToBottom函数,它通知应用程序何时开始BatchFetches内容、图片和检查。

   //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 的表格 View 。

我上网查了一下,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.
}

据我了解,这只会重新加载显示的单元格。但是,我的批量获取可能不会返回更多值。因此,我的表格 View 将显示与批量获取之前相同的单元格,这将重新加载这些单元格。由于这些单元格之前已加载,我不想浪费数据/搞乱我的表格 View 。

我该怎么办,谢谢?

最佳答案

您需要做的是计算需要重新加载的行,并确保在使用新数据更新数据源后将这些索引路径传递到重新加载行函数中。

 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) }
}

关于swift - 仅在新添加的单元格处重新加载 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58984790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com