Chr*_* G. 13 ios swift swift2 swift3
我正在尝试使用分页从后端加载数据.我已经看到了这个,但它加载了所有数据,然后是时间.这是正确的方式还是我做错了什么?
提前致谢.
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
}
Run Code Online (Sandbox Code Playgroud)
Soh*_*mon 34
不,你不能采用这种方法因为cellForRowAtIndexPath多次调用,而且需要很长时间来检查你的条件!
在这里,我找到了一个更好的UITableView分页选项.
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()
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
isNewDataLoading是Bool检查是否UITableView正在加载新数据!
希望这可以帮助!
你应该更多地实现加载tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath).当显示最后一个单元格加载时,表示用户滚动到底部,这时您需要加载更多数据.
也许它看起来像这样:
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if !(indexPath.row + 1 < self.TotalRowsWithPages) {
self.isLoading = true;
self.getmoredata()
}
}
Run Code Online (Sandbox Code Playgroud)