gpt4 book ai didi

iOS - 当用户到达 ScrollView 底部时加载项目

转载 作者:行者123 更新时间:2023-11-29 00:29:29 24 4
gpt4 key购买 nike

我目前正在使用 CloudKit 作为我的后端,到目前为止我非常喜欢它。

我正在进行一个查询,该查询检索数据以填充 TableView 。

由于我不知道它可能有的项目数,因此我将查询过滤为 X 个项目(假设为 15)。

我的目标是,当用户向下滚动到 TableView 的底部(最后查询的项目)时,我会查询后端以继续填充 TableView 。

我进行了搜索,但找不到执行此操作的 CloudKit 代码。

有人可以告诉我如何做吗?

感谢大家给予的帮助。最好的,伊万。

最佳答案

在 IOS 上,我认为您必须使用 UITableViewController 或仅使用 UITableView。

将 UIActivityIndi​​catorView(即微调器)添加到您的 UITableViewController。将 socket 连接到代码:

@IBOutlet weak var spinner: UIActivityIndicatorView!

向您的 UITableViewController 添加一个属性以跟踪您当前正在加载更多数据,这样您就不会尝试执行两次:

var loadingData = false

启动旋转器动画,然后调用 refreshRes():

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if !loadingData && indexPath.row == refreshPage - 1 {
spinner.startAnimating()
loadingData = true
refreshRes()
}

让 refreshRes() 在后台线程上运行。这将使您的 table 仍然可以自由移动。动画微调器将告诉用户更多数据即将到来。查询返回后,更新主线程上的表数据。

func refreshRes() {
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)) {
// this runs on the background queue
// here the query starts to add new 15 rows of data to arrays

dispatch_async(dispatch_get_main_queue()) {
// this runs on the main queue
self.refreshPage += 15
self.tableView.reloadData()
self.spinner.stopAnimating()
self.loadingData = false
}
}

之后取决于服务器结果,您必须发出请求以获取其他 15 个数据

关于iOS - 当用户到达 ScrollView 底部时加载项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42245677/

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