我有一个 API,当我从 Alamofire 发送请求时,它会返回我,例如 500 条记录!我不能把它全部拿走并使用它。相反,我在 API 上进行了分页,如 /page/1
、/page/2
等。
但是我怎样才能在我的 UITableView 中加载这些数据呢?我想在滚动 UITableView 时显示加载指示器,同时获取数据,并部分获取数据,将其附加到主数组中。我怎样才能实现这个目标?
或者还有其他解决方案吗?你能给我什么建议吗?
我搜索了很多相关内容,但大多数解决方案都是用 Objective-C 编写的。我找到了这个:iOS - How to make the tableview use paginated API output?
但我没有完全理解答案。那里有一些变量,我不知道它们的意思
最佳答案
您需要创建一个UIActivityIndicatorView并使用scrollViewDidScroll(scrollView: UIScrollView)来控制数据数组
private let serialQueue = dispatch_queue_create("mainVCLoadingQueue", DISPATCH_QUEUE_SERIAL)
override func scrollViewDidScroll(scrollView: UIScrollView) {
delegate?.mainCollectionViewDidScroll(scrollView)
}
func mainCollectionViewDidScroll(scrollView: UIScrollView) {
dispatch_sync(serialQueue, {
if scrollView.contentSize.height - scrollView.contentOffset.y - scrollView.frame.height < 1000 {
dispatch_async(GlobalMainQueue, {
loadingIndicator.startAnimating()
if !self.isLoading {
self.isLoading = true
self.itemDataManager!.getMoreItem({ itemList in
self.rootView?.data = itemList
self.isLoading = false
loadingIndicator.stopAnimating()
})
}
})
}
})
}
var data: YourData? {
didSet{
updateUI()
}
}
private func updateUI(){
collectionView.dataSource = data
collectionView.refresh()
}
关于ios - 如何从具有分页功能的 API 加载 UITableView 的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34660806/