I want to paginate data. What is the best way to add pagination when scrolling the tableview? I am trying not to showing all the data at once. For example, every time the user hits the bottom of the tableview the next 10 tableview cells load in (10, 20, 30 and so on.) I am using Swift and Firestore. Here is my code. thanks
import UIKit
import FirebaseCore
import FirebaseFirestore
import FirebaseAuth
class MainTableViewController: UIViewController {
var itemArray:[Item] = []
var item:Item!
override func viewDidLoad() {
super.viewDidLoad()
loadListingItems()
}
func ListingdownloadITemsFromFirebase(completion: @escaping (_
itemArray: [Item]) -> Void) {
var itemArray: [Item] = []
FirebaseReference(.Items).whereField(KISCAR, isEqualTo: true)
.order(by: kDATEPOSTED , descending: true).limit(to: 10
).getDocuments { (snapshot, error) in
guard let snapshot = snapshot else {
completion(itemArray)
return
}
if !snapshot.isEmpty {
for itemDict in snapshot.documents {
itemArray.append(Item(_dictionary:
itemDict.data() as NSDictionary))
}
}
completion(itemArray)
}
}
private func loadListingItems() {
ListingdownloadITemsFromFirebase { (allITems) in
print("we have ",allITems.count)
self.itemArray = allITems
self.tableView1.reloadData()
}
}
func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return itemArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt
indexPath: IndexPath) -> UITableViewCell {
cell.generateCell(itemArray[indexPath.row], indexPath:
indexPath)
}
}