2

I have UITableView with display caching data from array

I want to make pagination on UITableView like this structure:

1)When I go to view with UITableView - it's show me all caching data (this I did and it's works)

2) When user scroll down and if caching data left 5 call function whitch load new 10 data to array and show at the botom in UITableView and add recheck( when when table rows show 15 of 20 rows call function to load more data and add)

For example : in cach array I have 10 data, function load new and in table shows me 20 rows with data

I try to do this:

public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        print(indexPath.row)

        let lastItem = array.count - 1
        if indexPath.row == lastItem {
            loadMoreData()
        }
    }

 func loadMoreData {
        for _ in 0...9 {
            //load data from server
        }
       tableView.reloadData()
    }
Andrew0
  • 37
  • 1
  • 8
  • What is your problem? – SPatel Jun 20 '18 at 11:24
  • https://stackoverflow.com/a/47815332/2299040 – Sahil Manchanda Jun 20 '18 at 11:27
  • @SPatel how can I create check for left to 5 rows at the end and need to add more data? – Andrew0 Jun 20 '18 at 11:28
  • @SahilManchanda I dont need to send page number to server, Im need to load more – Andrew0 Jun 20 '18 at 11:30
  • Pagination should be done from backend. You just need to pass parameters along with `pageNo` to specify for which page you want data. So after api calling you need to append data in your current data source and increase page no. count by one. Page no. count could be managed by your own logic. – Mahendra Jun 20 '18 at 12:48

1 Answers1

3

Take one Bool Variable to check data is loading from api.

var isLoading = false

Add below code:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if !isLoading && indexPath.row == array.count - 5 {
        isLoading = true
        loadMoreData()
    }
}

set isLoading = false once you get data from api.

Hope this will help you :)

iVarun
  • 6,496
  • 2
  • 26
  • 34