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?
Thanks in advance.
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
}
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!Here, I have found a better option for
UITableView
pagination.isNewDataLoading
isBool
to check thatUITableView
is loading new data or not!Hope this helps!
Try to check more data not in cellForRowAtIndexPath but in UIScrollViewDelegate - [DataModel sharedMyLibrary] is my data source class loading video data using RESTful API with pagination, it's fetchWithCompletion method fetch data from server in async, it's hasMore method says that server has more data (JSON contains next link) LibraryTableViewController - is subclass of the UITableViewController, hasMore - is the view at the bottom of the table view in the storyboard containing a button, so user has two options: scroll down or press the button. Also if this view is visible indicates that there are more data on the server. _canFetch prevents nested loading from the server.
``
``
You should implement load more in
tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
. When last cell loading is display it mean user scroll to bottom so this is time you need load more data.Maybe it looks like this: