Guest User

Untitled

a guest
Feb 22nd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  2. return UITableViewAutomaticDimension
  3. }
  4.  
  5. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  6. return array.count;
  7. }
  8.  
  9.  
  10. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  11.  
  12. let cell = tableView.dequeueReusableCell(withIdentifier: "xxxxx", for:indexPath) as! xxxxxxxx
  13.  
  14. return cell;
  15. }
  16.  
  17.  
  18.  
  19. func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
  20. if scrollView == tableView {
  21. if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height) {
  22. print("Scroll Ended")
  23. fetchDataFromApi()
  24. }
  25. }
  26. }
  27.  
  28.  
  29. func fetchDataFromApi() {
  30.  
  31. let jsonUrlString = "xxxxxxx"
  32.  
  33.  
  34. guard let url = URL(string: jsonUrlString) else {
  35. return
  36. }
  37.  
  38. print(url)
  39.  
  40. URLSession.shared.dataTask(with: url) {(data,response,err) in
  41.  
  42. guard let data = data else {
  43. return
  44. }
  45. do {
  46. let apiResponseData = try JSONDecoder().decode(ApiResponse.self, from: data)
  47.  
  48. if apiResponseData.httpStatusCode == HttpStatusCode.OK {
  49.  
  50. if let apiResponse = apiResponseData.data{
  51. self.array.append(contentsOf: apiResponse)
  52. }
  53.  
  54.  
  55. DispatchQueue.main.async {
  56. self.tableView.reloadData()
  57. }
  58.  
  59. }
  60. else {
  61. print("API Response Error : (apiResponseData.httpStatusCode) (apiResponseData.errorMessage)")
  62. }
  63. }catch let jsonErr {
  64. print("Serialization Error (jsonErr)")
  65. }
  66. }.resume()
  67. }}
Add Comment
Please, Sign In to add comment