ios - Swift 在 UITableView 中创建分页

标签 ios swift uitableview pagination

我有 UITableView 并显示数组中的缓存数据

我想在 UITableView 上进行分页,如下结构:

1)当我使用 UITableView 进行查看时 - 它向我显示所有缓存数据(我这样做了并且有效)

2) 当用户向下滚动并且if caching data left 5调用函数将新的10个数据加载到数组并显示在UITableView底部并添加重新检查(当当表行显示 20 行中的 15 行时,调用函数来加载更多数据并添加)

For example : in cach array I have 10 data, function load new and in table shows me 20 rows with data

我尝试这样做:

public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        print(indexPath.row)

        let lastItem = array.count - 1
        if indexPath.row == lastItem {
            loadMoreData()
        }
    }

 func loadMoreData {
        for _ in 0...9 {
            //load data from server
        }
       tableView.reloadData()
    }

最佳答案

使用一个 bool 变量来检查数据是否从 api 加载。

var isLoading = false

添加以下代码:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if !isLoading && indexPath.row == array.count - 5 {
        isLoading = true
        loadMoreData()
    }
}

从 api 获取数据后设置 isLoading = false

希望这对你有帮助:)

关于ios - Swift 在 UITableView 中创建分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50946967/

相关文章:

ios - 为什么第一次加载不正确但刷新后正确加载?

ios - 如何使用 UITableView 和分段控件进行分页

ios - 将数据从 PopOver 传回 Viewcontroller

ios - 在设备上的应用程序上运行泄漏命令行工具

ios - CTFrameGetLineOrigins 返回 Y 坐标

uitableview - 在 Swift 中的自定义单元格中存储/检索核心数据

ios - 将 TableView 单元格添加到 View 中的 TableView

iphone - CBPeripheral Service 似乎是看不见的

ios - 无法在 swift 的子文件夹中找到图像?

iOS 在使用 Storyboard 推送 View Controller 时传递数据