本文介绍了UITableView 分页 - 底部刷新以在 Swift 中加载新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用分页从我的后端加载数据.我已经看到了这一点,但它一直在加载所有数据.这是正确的方法还是我做错了什么?

I am trying to implement loading data from my backend with pagination. I have seen this, but it loads all the data, all then time. Is this the right way or am I doing something wrong?

提前致谢.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    print(indexPath.row)

    if (indexPath.row + 1 < self.TotalRowsWithPages) {
        cell.textLabel?.text = "(self.myarray!.[indexPath.row].body)"
    } else {

        cell.textLabel?.text = "Loading more data...";

        // User has scrolled to the bottom of the list of available data so simulate loading some more if we aren't already
        if (!self.isLoading) {
            self.isLoading = true;
            self.TotalRowsWithPages = self.TotalRowsWithPages + self.PageSize
            self.getmoredata()
        }
    }

    return cell
}

推荐答案

不,你不能采用这种方法,因为 cellForRowAtIndexPath 被多次调用,而且检查你的条件!

Nope, you can't go with that approach because cellForRowAtIndexPath is called many times and also it will take much time to check your conditions!

在这里,我找到了一个更好的 UITableView 分页选项.

Here, I have found a better option for UITableView pagination.

func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {

    //Bottom Refresh

    if scrollView == tableView{

        if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height)
        {
            if !isNewDataLoading{

                if helperInstance.isConnectedToNetwork(){

                    isNewDataLoading = true
                    getNewData()
                }
            }
        }
    }
}

isNewDataLoadingBool 用于检查 UITableView 是否正在加载新数据!

isNewDataLoading is Bool to check that UITableView is loading new data or not!

希望这有帮助!

这篇关于UITableView 分页 - 底部刷新以在 Swift 中加载新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 09:22