iOS - 当用户到达滚动视图底部时加载项目

iOS - loading items when user reach the scroll view bottom

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

我正在执行一个查询,它检索数据以填充 table 视图。

因为我不知道它可能包含的项目数,所以我将查询过滤为 X 个项目(假设 15 个)。

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

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

有人可以提示我如何操作吗?

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

在 IOS 我认为你必须使用 UITableViewController 或只使用 UITableView。

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

@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 仍然可以自由移动。动画微调器将告诉用户更多数据即将到来。查询 returns 后,更新主线程上的 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 个数据