0

With some research I found this method quiete usefull to reach my purpose: pagination in a table view.

What I do is:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offsetY = scrollView.contentOffset.y
    let contentHeight = scrollView.contentSize.height
    
    if offsetY > contentHeight - scrollView.frame.height {

        //print

        if vievModel.!paginationFinished {
            self.viewModel.loadMoreData()
            self.tableView.reloadData()
        }
    }
}

The variable paginationFinished is manage in my viewModel, and when I call load more Data the page number is increasde by 1.

The problem is that this piece of code, when I scroll down to load more data, calls my viewModel function like all the possible times.

In fact if I print something in the line of my code it's printed like 15 times when i reach the end of my page.

What is the problem with this code? How can I improve this? I would like to reach the end of the page and call my loadMoreData once, so it loads the data in the pageCounter. I think i'm missing something

1

2 Answers 2

2

Add completion handler in loadMoreData() method & use a boolean check for call like below -

var isLoadingStarted = true

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offsetY = scrollView.contentOffset.y
    let contentHeight = scrollView.contentSize.height
    
    if offsetY > contentHeight - scrollView.frame.height {
        
        if vievModel.!paginationFinished && isLoadingStarted {
            isLoadingStarted = false
            self.viewModel.loadMoreData {
               self.tableView.reloadData()
               //self.isLoadingStarted = true
            }
            
        }
    }
}

//Handle loadMoreData method as below
static func loadMoreData(_ , completion:@escaping (() -> Void)) {
    .
    .
    .
    completion()
   
}

Use scroll view delegate to detect begin dragging and set flag isLoadingStarted there -

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    self.isLoadingStarted = true
}

Try it once.

Sign up to request clarification or add additional context in comments.

3 Comments

Your solution is great, but I still have the same problem. In loadMoreData I increment the page count by 1, and I manage correctly paginationFinished, but if I print page count when I scroll at the end of the page I see 1 , 2, 3, 4 etc. Which is the same behaviour =(
P.s. in my call to load more data I have page number and page size, so everything behind is working fine, the problem is here i think
@jamesCode updated my answer. Can you please try it?
1

you need to do some flagging

var isLoading = false

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offsetY = scrollView.contentOffset.y
    let contentHeight = scrollView.contentSize.height
    
    if offsetY > contentHeight - scrollView.frame.height && !isLoading {

        //print

        if vievModel.!paginationFinished {
            isLoading = true
            self.viewModel.loadMoreData()
            self.tableView.reloadData()
            isLoading = false
        }
    }
}

1 Comment

Could you please ready the comment I wrote to Shashank Mishra? I have the same scenario which is weird =/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.