1

I am using pagination for tableview, for that I have created spinner for tableview footer and calling that in scrollViewDidEndDragging but the spinner is not showing.. while pagination

code: with this code pagination is working but createSpinnerFooter is calling but not showing, why? where am i wrong. please guide me

 var currentPageNumberVM: Int = 1
 private func createSpinnerFooter() -> UIView {
    
    let footerView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 100))
    
    let spinner = UIActivityIndicatorView()
    spinner.center = footerView.center
    footerView.addSubview(spinner)
    spinner.startAnimating()
    return footerView
   }

   //and calling here
   func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {

     //Bottom Refresh
     if scrollView == resultTableView{

        if (((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height))
        //if scrollView.contentOffset.y > (resultTableView.contentSize.height-100-scrollView.frame.size.height)
        {
            resultTableView.tableFooterView = createSpinnerFooter()
            print("refresh called")
            self.refresh()
        }
    }
    }

   @objc func refresh() {
    if currentPageNumberVM <= searchData?.result?.jobs?.last_page ?? 1 {
        jobSearchResultServiceCall()//service call
        
    } else {
        self.view.makeToast("No more data to show")
    resultTableView.tableFooterView = nil

    }
   }

Edit: if i move resultTableView.tableFooterView = nil to else then issue is createSpinnerFooter is showing but once its load then cells not reloading i mean pagination is not working until i scroll again, why? where is the issue.

enter image description here

User123
  • 51
  • 1
  • 7

1 Answers1

1

It's because of resultTableView.tableFooterView = nil in your refresh() function that causes the spinner to be removed as soon as it was added.

Here is how

  1. First you add the spinner in scrollViewDidEndDragging with this line of code resultTableView.tableFooterView = createSpinnerFooter()
  2. Then you call self.refresh() immediately which I believe calls an API in the background
  3. However, in refresh you are also nil the footer view before the API call has completed resultTableView.tableFooterView = nil

All this happens so fast that you don't see the spinner being added and removed.

This line resultTableView.tableFooterView = nil should go in your else case and it should go in your API completion handler after the API has returned with a response or error

Update

You need to move resultTableView.tableFooterView = nil to two places.

One is in else block - OK, you have done this.

Another one is in the completion handler of your API call. It seems like your API call does not handle completion so I recommend you have a look at resource 1 and resource 2 to understand how to set up a completion handler for your API call.

Completion handler will let you know when you api call has completed and this is the second place you need to make the spinner to nil.

You need to change your jobSearchResultServiceCall() to have a completion handler using the links above.

Once this is done, you will have code that looks like this

@objc func refresh() {

    if currentPageNumberVM <= searchData?.result?.jobs?.last_page ?? 1 {

        jobSearchResultServiceCall { [weak self] _ in
            // your API call has finished and here is the second place
            // to remove the spinner
            self?.resultTableView.tableFooterView = nil
        })
        
    } else {
        self.view.makeToast("No more data to show")
        resultTableView.tableFooterView = nil
    }
}

Now your spinner gets dismissed if api call has completed or there are no more results to show

Shawn Frank
  • 4,381
  • 2
  • 19
  • 29
  • thank for the reply – User123 Feb 11 '22 at 17:01
  • if i move `resultTableView.tableFooterView = nil` to else then issue is `createSpinnerFooter` is showing but once its load then cells not reloading i mean pagination is not working until i scroll again, why? where is the issue. – User123 Feb 11 '22 at 17:01
  • Please see my updated answer. You need to introduce completion handler to your code. – Shawn Frank Feb 11 '22 at 18:10