1

EDIT: is this answer still valid?

In essence: iOS loads only the relevant cells - so it does lazy loading automatically


I would like to optimise the loading of a UITableViewController following the result of an API call to a web-service. To solve this I need to be able to load only a certain number of rows in a UITableViewController.

Here is my current strategy / understanding:

  1. The API call will retrieve the list of data available.
  2. The data is locally stored
  3. A notification is sent to UITableViewController warning the view controller to refresh the data
  4. At this point I would like to either refresh the relevant cells or to create a list of cells based on the data. Here is where I need most of your help (see below).

In order to do so I think I need to solve the following problem:

Loading table view partially (lazy loading):

I would load only the section of the table that is visible by the user plus another bit. How can be this made?

The function below normally uses the count of the data available... can we use somehow a lazy loading approach on this (e.g. loading only 20 rows and not 200?)?

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

Any help?

Community
  • 1
  • 1
mm24
  • 9,280
  • 12
  • 75
  • 170
  • `myarray` is supposed to contain always the total number of items. The creation of the model items without the time consuming data / properties is not expensive. Lazy loading means to load that time consuming data asynchronously on demand and update the UI accordingly. `tableView:cellForRowAtIndexPath` is always called when the user scrolls and new cells are going to be displayed so that's the location to start the lazy loading. – vadian Jan 15 '17 at 18:18
  • 2
    YES, the answer u found is still valid. – LoVo Jan 15 '17 at 18:31
  • @LoVo cool.. so there seem to be no need for me to do extra work on the UITableViewController.. or shall I? What are the things that people do to optimise the initial loading? – mm24 Jan 15 '17 at 18:40
  • I can´t tell you if or how you need to improve your initial loading because you didn't added any code but if your cell ui isn`t too fancy you shouldn't have any problems ;) – LoVo Jan 15 '17 at 18:44
  • And what if my cell UI was fancy? Which strategy would you suggest? Is there a good tutorial out there? – mm24 Jan 15 '17 at 18:50

0 Answers0