Load all table view cell images all together in iOS - ios

I want to load all table view cell images all together instead of waiting for scroll down. The images are loading from server. I can load all the images as data and store them in an array and show them. But is there any other way for this? Any kind of help would be really appreciated.

You could fetch the images asynchronously in the viewDidLoad method, that way the user does not have to wait and can immediately start interacting with the UI. As long as the images haven't loaded (i.e. images array size is 0) you could show a generic image. Once you are done fetching you can update the tableview by calling the reloadData method:
For efficiency, the table view redisplays only those rows that are visible
As the user starts scrolling in the tableview, any subsequent loaded cells will now be able to access the populated images array in the cellForRowAtIndexPath method.

Related

Swift stop unloading of table view cells?

I'm making a table view that downloads an image for the cell off the web, every time the cell is loaded.
This proves a problem though, as when you scroll down then up again, the cells are reloaded.
Is there any way to stop the unloading of cells, and control how many cells are loaded when the view appears?
I propose that instead of trying to stop your UITableView from reusing cells, which would decrease performance, you store the images you download, using NSCache for example.
NSCache works the same way as a Dictionary - with key value pairs. The main difference however is it incorporates various auto-removal policies, which ensure that it does not use too much of the system’s memory. Here's the documentation on NSCache: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/NSCache_Class/index.html
Using this method: when you're setting up your UITableViewCell, you could check if an image has been downloaded for that NSIndexPath. If an image hasn't been downloaded you download it and add it to the cache once its finished, otherwise you use your cache and get the image from there.

Cancel NSOperations based on position in TableView/CollectionVIew

Suppose I have a table view or a collection view. In the cells I would need to load certain content (eg. an Image). I can get the content from local storage (if it has been saved there) or from the internet. Either way loading the content takes a while (500 kB Image).
The best way to do this is to create an NSOperation that will load in the background and then call a delegate when it finishes.
But suppose the user enters the table view / collection view and the queues start to get the first set of content (1-10) but then the user scroll quickly right to the end (visible cells 100-110) what should I do? The start-download operation happens when the cell is presented (cellForRowAtIndexPath) so I need to wait for everything to download until the user (who is now at the end of the tableView) sees content.
I tried creating a queue on each cell and cancelling that in prepareForReuse, but that crashes the app.
Anyway if the user is in a section of the tableview and content is downloading, but not shown yet, and scrolls further causing the download to be cancelled and then returns. Then he will need to download the first half of the image again, which is not good on limited data plans.
What is the best way to handle such a situation?
You could have your controller class adopt the UITableViewDelegate protocol, which will allow you to inject custom code into –tableView:willDisplayCell:forRowAtIndexPath: (called for you by the table view when a cell is to be displayed). In it you could calculate that because of the screen size, only rows within k indices of any row being shown could possibly be visible; thus, in this method call, cancel all the load operations associated with rows outside that range (relative to indexPath argument).

Display cells while others load in the UITableView?

Right now my table view presents 5 cells at the same time. I load them all up into an array so the "flow" of the UITableView is easier. But since there quite a few objects, the initial load can take quite a bit.
So my question is, is there a way to present the initial 5-7 cells while the rest are loading? Or what would be the practice for this?
The idea: for the first few cells to come up as fast possible, even while we are loading a bunch in the background so the user isn't sitting there waiting for 100+ cells to load.
Also, I am loading this cells with parse (I am getting the user's info including an image).
Thanks!
This may not work depending on where your data comes from and when it gets into your app, but this is what I would do. Allow your array to fill up with the first 5-7 items. Then call reloadData on your table view. Assuming your datasource is hooked up to your array properly, it will load the first few items. Then let your array continue to load until it's complete, and call reloadData again. You could even use this strategy repetitively to continually load new data. Good luck!

How can I make my UITableView request the X next cells that are outside the visible table?

I would like to have my UITableView load 3 or 4 cells outside of the table so that any data to be shown there is already loaded when I scroll down.
I have some images, and data that must be downloaded before it can be shown in the cell.
This causes a visible delay before the images are loaded when scrolling.
I can manually trigger loading of this data by doing it in the UITableViewDataSource tableView:cellForRowAtIndexPath: method. I've done this before, but I'm curious if there's an easier way to do it.
Is there any way to expand the reusable cell pool, or adjust how the cells are loaded/recycled?
EDIT:
To clarify, I have lazy loading of images and data in place.
Everything works fine, I just wanted an easy solution to the "prefetching" problem.
Which can be solved in many ways that has nothing to do with the view itself. Right now you can easily see the images load as you scroll. I just wanted them to load right before they become visible.
You may be looking for an asynchronous table view that loads the data asynchronously.
Apple provides a sample app demonstrating this:
LazyTableImages
Of course, you could pre-cache the data and begin downloading data into your datasource before they scroll.
The general idea is that you are loading data into a datasource (that is separate from the UI), so you can do this at any time (and in the background). You can display temporary data or some type of loading image or spinner if the data isn't loaded yet.
If data of the cell will be loaded when cel becomes visible, you can programmatically scroll the table view by scrolling to the bottom cell and go back to the first cell without animation. Another way would be creating all the cells and placing them into array when your view controller is created, and feed the table from that array that contains already created cells. I think there is no way to extend the cell pool as you are asking. Hope this helps, Good Luck!

How can I load a large amount of data in a UITableview without memory warnings?

I'm trying to create a custom view that can display a large amount of data, similar to the way UITableView is able to display many rows of data. Right now I'm displaying the data in a UIScrollView, but after I reload the data several times my app starts to receive memory warnings.
What's the right way to design a custom view to display a large amount of data, and how can I avoid these memory warnings?
It doesn't. UITableView loads only as many rows as it can fit on the screen. When it needs more rows it asks the data source for them. This gives the appearance that the table contains all the data without all the overhead of copying everything at once.
When your tableView cell is too heavy means, it has imageView and more than one text label better create a custom cell by subclassing the UITableView.
Make sure that in custom cell the subview should be added only once and reused for cells. Just you need to sent the image of imageview or text of label. Even if you have so many cells in tableview the performance will be good.
And also make sure that if you are loading images from server don't fetch every time when you are setting image for cell. use image cache.
The following link might me useful for loading data to table view from server.
http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial

Resources