0

I'm trying to create an app where the user scrolls to the bottom of the tableview more results are displayed. At present only 20 items are displayed as per the API. The API has a parameter for page which I'm trying to increment by 1 each time the user scrolls to the bottom via this in the ViewController.

When I open the app it loads me page 1, when I go to the bottom of the tableView it quickly loads me in sequence page 2, 3, 4, 5 and stops at page 5. All the other pages are not displayed anymore, only page 5 is seen.

How can I see all the pages together after scrolling down?

ViewController.swift

override func viewDidLoad() {

        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self

        swiftManager.delegate = self

        swiftManager.fetchUrl()
        
    }


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

SwiftManager.swift

struct SwiftManager {

let baseURL = "https://api.themoviedb.org/3/movie/popular"
    let apiKey = "xxxxxxxxxxxxx"
    let filterURL1 = "language=en-US"
    let filterURL2 = "sort_by=popularity.desc"
    let filterURL3 = "page="
    var filterPage = 1



func fetchUrl() {
        
      let urlString = "\(baseURL)?api_key=\(apiKey)&\(filterURL1)&\(filterURL2)&\(filterURL3)\(filterPage)"
        
       performRequest(with: urlString)
        
        print("Number page = \(filterPage)")
        
    }


mutating func fetchUrlLoadMore() {

      while (filterPage<5) {
            
      let urlString = "\(baseURL)?api_key=\(apiKey)&\(filterURL1)&\(filterURL2)&\(filterURL3)\(filterPage)"
        
       performRequest(with: urlString)
            
        filterPage += 1
            
            print("Number page = \(filterPage)")
            
        }
        
    }
SwiftLove
  • 31
  • 1
  • 6
  • What's the code of `performRequest(with:)`? You should keep a var to let you know if you are currenlty fetching or not and avoid multi fetch if already currently fetching: See https://stackoverflow.com/questions/35495750/uitableview-pagination-bottom-refresh-to-load-new-data-in-swift – Larme Feb 26 '21 at 12:52
  • What's the code of `didUpdateStruct(_: swiftData:)`? Do you replace old values with new ones? – Larme Feb 26 '21 at 16:03
  • Can anyone help me please? – SwiftLove Feb 28 '21 at 11:47
  • So, I asked: What's the code of `didUpdateStruct(_: swiftData:)`, and also what's the code of `tableView(_:cellForRow:atIndexPath:)`, of `performRequest(with:completion:)`? You seem to override your data, but there is not enough code to tell why. – Larme Mar 02 '21 at 08:52

0 Answers0