Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27133

Update data source of an UITableView with new data after scrolling table to the bottom

I have backend that return data based on the parameters. So the parameters include offset and limit keys.

It is something like pagination if you know. So based on the offset and limit server return to me appropriate amount of item from the data base.

At first run, I load 30 items from the 0 offset position. Then I need to load more items after I scrolled down my table and update datasource.

I have found the solution but the problem will be as well not to duplicate data when I will scroll up and then one more scroll down. Maybe there is already made solution or example. Of course I can write some my "wheel", but maybe some one can suggest best algorithm, maybe also using scroll view delegate it is not so good, instead of this maybe better use cellForRowAtIndexPath to determine for example some cell that near to the end of the cells and then update it.

Upvotes: 0

Views: 892

Answers (1)

Andy Obusek
Andy Obusek

Reputation: 12832

I've solved this same problem with using willDisplayCell and the following two conditions that control whether more items are loaded:

  • Use a condition to check that the "total" number of items loaded from the API is greater than the number of rows already in the UITableView
  • Use a condition to check that the "row to display" is at a count that is greater than the number of rows already loaded

Basically, it's a slight modification of this answer: https://stackoverflow.com/a/19448220/209867

Upvotes: 2

Related Questions