0

I searched around for pagination but everything seems to point to pulling data from a server:

Pagination

Pagination

In my situation my data arrays are filled with static items wherein as I already know the count of what's inside of each array. My tabBar has three tabs and each tab has a tableVIew with different amounts of data

tableDataForTabOne = [DataModel]() // the array has 1000 items in it
tableDataForTabTwo = [DataModel]() // the array has 690 items in it
tableDataForTabThree = [DataModel]() // the array has 7 items in it

How do I paginate the arrays for the tableView into different pages? For example the first 10 items is 1 page, the next 10 items is another page, etc etc?

The question has nothing to do with the tabs. I don't know how to paginate on a tableView without pulling data from a server.

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak fileprivate var tableView: UITableView!

    let tableDataForTabTwo = [DataModel]() //has 690 items in it
    var pageNumber = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        ...
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableDataForTabTwo.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! MyCell

        cell.titleLabel.text = tableDataForTabTwo[indexPath.row]."some static title"
        cell.imageView.image = tableDataForTabTwo[indexPath.row].UIImage(named: "some static image")

        return cell
    }

}
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
  • Why would you want to? – dan Apr 19 '18 at 17:19
  • @dan thanks. If you notice tableDataForTabOne has 1000 items in it vs tableDataForTabThree has 7 items in it. I don't want the tableView's scroll to lag since tableDataForTabOne has all those items in it – Lance Samaria Apr 19 '18 at 17:22
  • 1
    It won't lag, tableviews only create enough cells to cover the number that will be shown at one time and reuse them as you scroll. – dan Apr 19 '18 at 17:23
  • I read that tableViews or collectionViews lag when the data source has to many items in it – Lance Samaria Apr 19 '18 at 17:26
  • @dan maybe my confusion is pulling the data from a server vs pulling static data. If that isn't a problem then I'm good – Lance Samaria Apr 19 '18 at 17:29
  • pulling data from server will save RAM, static data will consume more RAM to save the data in array. As for tableview, it wouldn't matter if it is static/server driven. – Aju Antony Apr 19 '18 at 17:31
  • TableView lags while scrolling, only when there are lot of processing going on during 'cellForRow:atIndexPath' function call. You can either pre fetch and save all data, or make it asynchronous so that processing happens in background. As far as I know, a large data source shouldn't effect the tableview scrolling performance. Source: Dude, trust me XD. (No Seriously, I've done huge data pagination for multiple apps and the lag was never because of huge data source, always because of too much processing being done while scrolling.) – Aju Antony Apr 19 '18 at 17:35
  • @CoderFrom94 thanks for the info. What would cause “too much processing” -images being to big? – Lance Samaria Apr 19 '18 at 17:37
  • that can be subjective, in my last app, the lag was caused because we had created a system for localization where the string would be initialized from a struct which would call a localize function that would provide the appropriate string based on the selected language. There was no image processing at all, just string processing and initializations (from CoreData). Honestly, you'll have to analyse this for every project. It could be a range of issues. In one of my previous projects it was because we were applying filters on images, etc. – Aju Antony Apr 19 '18 at 17:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169363/discussion-between-coderfrom94-and-lance-samaria). – Aju Antony Apr 19 '18 at 17:48
  • @CoderFrom94 I started driving. When I get to my destination I will message you back. Thanks – Lance Samaria Apr 19 '18 at 17:53

0 Answers0