gpt4 book ai didi

ios - Swift 在 UITableView 中创建分页

转载 作者:行者123 更新时间:2023-11-28 14:35:00 26 4
gpt4 key购买 nike

我有 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/

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