Search code examples
iosswiftuiscrollviewinfinite-scroll

UIScrollView: infinite scroll to display fibonacci numbers


I am trying to let the user infinitely scroll the screen for more fibonacci numbers. I can only display up to the number of fib numbers that is the length of my posts array. Here is what it looks like now. enter image description here I am having trouble implementing the scrollViewDidScroll function to achieve the infinite scroll. I found some code on stackoverflow that makes sense to me, but I don't know what to do to connect it to the tableview (the part where you call for more data). Any input is appreciated!

import UIKit

class FeedTableViewController: UITableViewController {

    let posts : [String] = ["","","","","","","",""]
    var fibArray: [Int] = [0,1]
    let cellIdentifier = "userPostFeedCell"

    var indexOfPageToRequest = 1
    override func scrollViewDidScroll(_ scrollView: UIScrollView) {

        let offsetY = scrollView.contentOffset.y
        let contentHeight = scrollView.contentSize.height

        if offsetY > contentHeight - scrollView.frame.size.height {

            // increments the number of the page to request
            indexOfPageToRequest += 1

            // call your API for more data


            // tell the table view to reload with the new data
            self.tableView.reloadData()
        }


    }

    func fibonacci(n: Int) -> Int {
        if (fibArray.count > n) {
            return fibArray[n];
        }

        let fibonacciNumber = fibonacci(n: n - 1) + fibonacci(n: n - 2)
        fibArray.append(fibonacciNumber)
        return fibonacciNumber
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.register(
            UINib(nibName: "FeedTableViewCell", bundle: nil), forCellReuseIdentifier: cellIdentifier)
    }


    override func numberOfSections(in tableView: UITableView) -> Int {

        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return posts.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> FeedTableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)

        cell.textLabel?.text = "\(fibonacci(n: indexPath.row+1))"
        print("cellForRowAt \(indexPath.row)")


        return cell as! FeedTableViewCell
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        print("heightForRowAt \(indexPath.row)")
        return 40
    }
}

Solution

  • You'll overflow an Int pretty quick in a fibonacci series so you don't actually need infinite scroll. You just set the number of rows in your section to be high. The application only ever creates the cells that need to be displayed so you won't be using large amount of memory. Here is my code

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20000
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
        cell.textLabel?.text = "\(fibonacci(n: indexPath.row+1))"
        return cell
    }