0

I have a list of items from https://api.myjson.com/bins/1h5pdq that I want to display to my tableview. I am using alamofire 4.5. I am able to load everything all at once in the table. I am just wondering if its possible to have a view more option in alamofire that loads 5 or 10 cells at a time. I can put a button on the last cell for view more function but I am not so sure how to limit the response from alamofire to first 10 items first then if I press view more it will load another 10 so on. Thanks for your input.

jiren
  • 7
  • 6

2 Answers2

1

Alamofire has no settings to do what you want , you have to do it manually , or fetch all elements like what you currently do , say you have 50 items in the fetched array

var countToShow = 10

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return countToShow

}

and when more clicked increase countToShow and reload the table , you can also have a look to this

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Hi. I tried this approach but the cellForRowAt function I assign the value of the object to the cell label gives me an index out of range error – jiren Jul 15 '18 at 15:05
  • make sure countToshow is always less than the array count – Shehata Gamal Jul 15 '18 at 15:07
  • I have an array count of 100 and when I set the return value to 50 it still shows the index out of range error – jiren Jul 15 '18 at 15:09
  • but when I return lets say myarray.count - 90 it shows 10 rows. I just tried to return the same value but different approach on this – jiren Jul 15 '18 at 15:12
  • maybe because the array is not yet fetched , make sure to change the value to 10 when the response returns – Shehata Gamal Jul 15 '18 at 15:13
  • can you please elaborate on your answer. I did try to put the fetch function to viewwillappear to avoid this out of range but that seems to be the incorrect approach since I still encountered the error. Thank you for all your help – jiren Jul 15 '18 at 15:17
  • declare countToShow = 0 , inside completion block set it to 10 and reload the table – Shehata Gamal Jul 15 '18 at 15:26
0

Alamofire is only a library for networking. The task you want to accomplish requires concepts of pagination. Kind of like infinite scrolling. You need to maintain the total number of records available to display, how many you want to fetch at a time, how many you want to display at a time before the next request is hit.

https://www.raywenderlich.com/187041/uitableview-infinite-scrolling-tutorial

(P.S. - Its a link to a tutorial and more likely to be updated for newer releases than be changed)

Yash Tamakuwala
  • 1,789
  • 1
  • 24
  • 33