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