Infinite Scroll with UITableView and Array

I am new to Swift and I want to create an infinite scroll with an array. Here he is my class TableViewController

class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
  var legumes: [String] = ["Eggs", "Milk", "Chocolat", "Web", "Miel", "Pop", "Eco", "Moutarde", "Mayo", "Thea", "Pomelade", "Gear", "Etc" , "Nop", "Dews", "Tout", "Fun", "Xen" , "Yoga" ]
  override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.delegate = self
    self.tableView.dataSource = self
    func numberOfSections(in tableView: UITableView) -> Int {
      return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return self.legumes.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ImageTableViewCell
      return cell
    }
}

I want to show the first ten elements of my array, and when I am at the bottom of the TableViewController, it will load the next ten elements, etc. I don’t know how to do this, I see a lot of code on GitHub, but I don’t know how to implement them.

Thank you very much.

+4
source share
2 answers

What you describe is called pagination. You should do something like this:

/* Number of page you're loading contents from */
var pageIndex: Int = 1

override func scrollViewDidScroll(scrollView: UIScrollView) {
    let offsetY = scrollView.contentOffset.y
    let contentHeight = scrollView.contentSize.height

    if offsetY > contentHeight - scrollView.frame.size.height {
        /* increment page index to load new data set from */
        pageIndex += 1

        /* call API to load data from next page or just add dummy data to your datasource */
        /* Needs to be implemented */
        loadNewItemsFrom(pageIndex)

        /* reload tableview with new data */
        tableView.reloadData()
    }
}
0
source

Consider Using PagingTableView

class MainViewController: UIViewController {

  @IBOutlet weak var contentTable: PagingTableView!
  var legumes: [String] = ["Eggs", "Milk", "Chocolat", "Web", "Miel", "Pop", "Eco", "Moutarde", "Mayo", "Thea", "Pomelade", "Gear", "Etc" , "Nop", "Dews", "Tout", "Fun", "Xen" , "Yoga" ]

  override func viewDidLoad() {
    super.viewDidLoad()
    contentTable.dataSource = self
    contentTable.pagingDelegate = self
  }

}

extension MainViewController: UITableViewDataSource {

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return legumes.count
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ImageTableViewCell
    guard legumes.indices.contains(indexPath.row) else { return cell }
    cell.content = legumes[indexPath.row]
    return cell
  }

}

extension MainViewController: PagingTableViewDelegate {

  func paginate(_ tableView: PagingTableView, to page: Int) {
    contentTable.isLoading = true
    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
      self.legumes.append(contentsOf: legumes)
      self.contentTable.isLoading = false
    }
  }

}

paginate ,

0

Source: https://habr.com/ru/post/1672931/


All Articles