2

I am trying to implement pagination in table view. so when fetching data from server, I want to get 10 data per request.

so the first time user opens the view, it will fetch 10 data, after the user scrolls to the 10th row, it will send request to get the data 11st to 20th.

but after fetching that 11-20th data and reloading the table view, it seems the table view drag down and it shows the last row (i.e the 20th row).

I want after the 10th row --> fetching data --> show the 11st row of the table view

so it will give smooth scrolling and good user experience

here is the simplified code i use. Thanks in advance

    let numberOfDocumentsPerQuery = 5
    var fetchingMore = false
    var recommendedEventList = [EventKM]()
    var lastDocumentFromQuery : QueryDocumentSnapshot?

    extension HomeVC : UITableViewDelegate, UITableViewDataSource {
        //MARK: - Table View Methods

        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }

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

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "HomeCell", for: indexPath) as! HomeCell
            cell.eventData = recommendedEventList[indexPath.row]
            return cell
        }


        func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {

            // to fetch more events if it comes to the bottom of table view

            let currentOffset = scrollView.contentOffset.y
            let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height

            if maximumOffset - currentOffset <= 10.0 {
                if !fetchingMore {
                    getRecommendedEvents()
                }
            }
        }

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            performSegue(withIdentifier: "toEventDetailVC", sender: nil)
        }

    }

 func getRecommendedEventsFromBeginning() {

        EventKM.observeRecommendedEvents (

        selectedCity: selectedCity,
        limit: numberOfDocumentsPerQuery) { (recommendedList, listener, lastDocument) in

            if let events = recommendedList {
                self.recommendedEventList = events
                self.tableView.reloadData()

                self.lastDocumentFromQuery = lastDocument
                self.recommendedListener = listener
                self.fetchingMore = false

            } else {
                self.fetchingMore = true // to stop fetching data
            }


        }

    }
sarah
  • 3,819
  • 4
  • 38
  • 80
  • check this https://stackoverflow.com/questions/5856427/how-to-scroll-uitableview-to-specific-position scroll your tableview to specific position – Muhammad Shauket Aug 06 '18 at 02:13

2 Answers2

4

You can have a property to record the row that scroll to.
var rowForScroll = 1

every time you loaded data, add the rowForScroll

rowForScroll = rowForScroll + 10
let ip = NSIndexPath(forRow: rowForScroll, inSection: 0)
self.tableView.scrollToRowAtIndexPath(ip, atScrollPosition: .bottom, animated: true)
NF Lee
  • 462
  • 3
  • 12
0
// Add your custom object.
recommendedEventList.add(customObject)

let numberOfRowsBeforeAPIRequest = tableView.numberOfRows(inSection: section)
let indexPath:IndexPath = IndexPath(row:(recommendedEventList.count - 1), section:0)
tableView.insertRows(at:[indexPath], with: .none)

// Do Validate if the actual indexpath exists or not, else will crash.
let ip = NSIndexPath(forRow: numberOfRowsBeforeAPIRequest + 1, inSection: 0)

tableView.scrollToRowAtIndexPath(ip, atScrollPosition: .bottom, animated: true) 

Just putting what I am trying to do above.

  1. Saving the number of Rows before reloading.
  2. Adding new rows after the last row of the section.
  3. Then scrolling 1 row down in the tableview.
Karthick Ramesh
  • 1,451
  • 20
  • 30