我UITableView
显示数组中的缓存数据
我想像这样的结构对UITableView
进行分页:
1(当我去查看UITableView
时 - 它向我显示所有缓存数据(我做了并且它有效(
2(当用户向下滚动并调用函数if caching data left 5
将新的10个数据加载到数组中并在UITableView
的botom上显示并添加recheck(当表行显示20行中的15行时,调用函数以加载更多数据并添加(
例如:在 cach 数组中,我有 10 个数据,函数加载新和在 表格显示 20 行数据
我尝试这样做:
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()
}
取一个布尔变量来检查数据是否从 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
。
希望这对你有所帮助:)