For that you need to have server side change also.
-
Server will accept
fromIndexandbatchSizein theAPIurl as query param.let listUrlString = "http://bla.com/json2.php?listType=" + listType + "&t=" + NSUUID().UUIDString + "&batchSize=" + batchSize + "&fromIndex=" + fromIndex -
In the server response, there will be an extra key
totalItems. This will be used to identify all items are received or not. An array or itemsfromIndextobatchSizenumber of items.
In the app side
-
First
loadItem()will be called withfromIndex = 0andbatchSize = 20(for example inviewDidLoad()orviewWillAppear). removeAll items fromprivateListarray before callingloadItem()for the first time -
Server returns an array of first 20 items and
totalItemstotal number of items in the server. -
Append the 20 items in
privateListarray and reloadtableView -
In
tableView:cellForRowAtIndexPathmethod check if the cell is the last cell. And check iftotalItems(form server) is greater thanprivateList.count. That means there are more items in the server to loadif indexPath.row == privateList.count - 1 { // last cell if totalItems > privateList.count { // more items to fetch loadItem() // increment `fromIndex` by 20 before server call } }
Question: where is refresh ? will be scrolling ?
Refresh after appending new items in the array when server response received. (step 3)
Scrolling will trigger tableView:cellForRowAtIndexPath for every cell when user scrolls. Code is checking if it is the last cell and fetch remaining items. (step 4)
Sample project added:
https://github.com/rishi420/TableViewPaging