Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class TestViewController: UIViewController {
- @IBOutlet weak var tableView: UITableView!
- var dataList: [String] = [String]()
- var fromIndex: Int = 0
- let totalEntries: Int = 100
- let limit: Int = 20
- override func viewDidLoad() {
- super.viewDidLoad()
- tableViewSetup()
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- dataList.removeAll()
- loadMoreItems()
- }
- @objc func loadMoreItems() {
- let endIndex = min(totalEntries, fromIndex + limit)
- for i in fromIndex..<endIndex{
- dataList.append(String(i))
- }
- fromIndex = endIndex
- tableView.reloadData()
- // Dismissing activity indicator from footer...
- self.tableView.tableFooterView = nil
- }
- }
- extension TestViewController: UITableViewDelegate, UITableViewDataSource {
- func tableViewSetup() {
- tableView.dataSource = self
- tableView.delegate = self
- tableView.rowHeight = 40
- }
- func numberOfSections(in tableView: UITableView) -> Int {
- return 1
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return dataList.count
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
- return cell
- }
- func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
- cell.textLabel?.text = dataList[indexPath.row]
- if indexPath.row == dataList.count - 1 && totalEntries > dataList.count{
- // Adding Activity Indicator as footerView in tableView
- let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
- spinner.startAnimating()
- spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44))
- self.tableView.tableFooterView = spinner
- self.tableView.tableFooterView?.isHidden = false
- // Call loadMore() method
- self.perform(#selector(loadMoreItems), with: nil, afterDelay: 2.0)
- }
- }
- }
Add Comment
Please, Sign In to add comment