Guest User

Untitled

a guest
Jan 24th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. import UIKit
  2.  
  3. class TestViewController: UIViewController {
  4.  
  5. @IBOutlet weak var tableView: UITableView!
  6. var dataList: [String] = [String]()
  7. var fromIndex: Int = 0
  8. let totalEntries: Int = 100
  9. let limit: Int = 20
  10.  
  11. override func viewDidLoad() {
  12. super.viewDidLoad()
  13. tableViewSetup()
  14. }
  15.  
  16. override func viewWillAppear(_ animated: Bool) {
  17. super.viewWillAppear(animated)
  18. dataList.removeAll()
  19. loadMoreItems()
  20. }
  21.  
  22. @objc func loadMoreItems() {
  23.  
  24. let endIndex = min(totalEntries, fromIndex + limit)
  25. for i in fromIndex..<endIndex{
  26. dataList.append(String(i))
  27. }
  28. fromIndex = endIndex
  29. tableView.reloadData()
  30.  
  31. // Dismissing activity indicator from footer...
  32. self.tableView.tableFooterView = nil
  33. }
  34. }
  35.  
  36. extension TestViewController: UITableViewDelegate, UITableViewDataSource {
  37.  
  38. func tableViewSetup() {
  39. tableView.dataSource = self
  40. tableView.delegate = self
  41. tableView.rowHeight = 40
  42. }
  43.  
  44. func numberOfSections(in tableView: UITableView) -> Int {
  45. return 1
  46. }
  47.  
  48. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  49. return dataList.count
  50. }
  51.  
  52. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  53. let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
  54. return cell
  55. }
  56.  
  57. func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  58. cell.textLabel?.text = dataList[indexPath.row]
  59. if indexPath.row == dataList.count - 1 && totalEntries > dataList.count{
  60.  
  61. // Adding Activity Indicator as footerView in tableView
  62. let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
  63. spinner.startAnimating()
  64. spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44))
  65. self.tableView.tableFooterView = spinner
  66. self.tableView.tableFooterView?.isHidden = false
  67.  
  68. // Call loadMore() method
  69. self.perform(#selector(loadMoreItems), with: nil, afterDelay: 2.0)
  70.  
  71. }
  72. }
  73. }
Add Comment
Please, Sign In to add comment