UITableView分页 - 底部刷新以在Swift中加载新数据 [英] UITableView Pagination - Bottom Refresh to Load New Data in Swift
问题描述
我正在尝试使用分页从后端加载数据。我已经看到了这个,但它加载了所有数据,然后是时间。这是正确的方法还是我做错了什么?
I am trying to implement loading data from my backend with pagination. I have seen this, but it loads all the data, all then time. Is this the right way or am I doing something wrong?
提前致谢。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
print(indexPath.row)
if (indexPath.row + 1 < self.TotalRowsWithPages) {
cell.textLabel?.text = "\(self.myarray!.[indexPath.row].body)"
} else {
cell.textLabel?.text = "Loading more data...";
// User has scrolled to the bottom of the list of available data so simulate loading some more if we aren't already
if (!self.isLoading) {
self.isLoading = true;
self.TotalRowsWithPages = self.TotalRowsWithPages + self.PageSize
self.getmoredata()
}
}
return cell
}
推荐答案
不,你不能采用这种方法,因为<多次调用code> cellForRowAtIndexPath 并且还需要很长时间来检查你的条件!
Nope, you can't go with that approach because cellForRowAtIndexPath
is called many times and also it will take much time to check your conditions!
在这里,我找到了一个更好的选择 UITableView
分页。
Here, I have found a better option for UITableView
pagination.
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
//Bottom Refresh
if scrollView == tableView{
if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height)
{
if !isNewDataLoading{
if helperInstance.isConnectedToNetwork(){
isNewDataLoading = true
getNewData()
}
}
}
}
}
isNewDataLoading
是 Bool
检查 UITableView
正在加载新数据!
isNewDataLoading
is Bool
to check that UITableView
is loading new data or not!
希望这会有所帮助!
这篇关于UITableView分页 - 底部刷新以在Swift中加载新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!