Preload tableView cells and prevent reloading - ios

I already found entries with that topic on this page and also a website that provides a tutorial for that problem. But nothing worked very well.
The tutorial sad I should double the height of my tableView so cells loaded earlier, but with a higher tableView I never reached the last cells.
My problem is, I use a tableView to display my apps mainpage. This mainPage shows every time a topic and if its necessary for that topic it shows for example a cell with a map, one with pictures and sometimes not. Problem now, if I trying to scroll to my view its always lagging because it loads a map or this pictures. And scrolling back again because the loaded cells already deleted. I used a tableView because of the possibility to switch celltypes(mapCell, pictureCell, textCell) on and off.
I understand this feature, because of memory issues but in my case its not that much and it would be better if all my cells be preloaded and stay in memory until I change the topic.
Is there a swifty way to told my tableView to have this behavior?
Thanks a lot and nice greetings

I would suggest a different approach. Set up your table view controller to install placeholder images in your cells, trigger an async download that you cache to disk, and then update the cell with it's downloaded content if it's still visible when the download is finished.
There are various third party frameworks that do all this housekeeping for you.

Declare a cell array.
Follow these steps whenever change in your topic.
Clear you array
Create and configure all cells add to your array.
Return these cells to datasource methods using row index. Don't use tableview dequeue method here.

Related

Maintain collectionview speed after reloading data in UICollectionView

I am implementing a chat app using UICollectionView. Initially I fetch only 100 messages. As the user scrolls, I fetch some more messages and then reload the collection view. To maintain the scroll position I set the content offset but scrolling gets stopped. How to maintain the scrolling speed after reloading the UICollectionView so user doesn't feel jerk?
You could try to use the prefetchDataSource that was introduced in iOS10.
UICollectionView Prefetch Data Source in iOS 10?
There're several approaches to handle this.
You don't use reloadData() but rather insertItemsAtIndexPaths:, that way you don't have rebuild the whole table and only build the items you need.
You load additional data when use hits a specific point your collection view. In a willDisplayCell method you can check if indexPath of a cell is getting close to the last index in your data source and load additional rows. In the moment user will scroll to the last item, all the stuff will be already there so you don't have worry.
In my experience the best way is to use both of this approaches combined. Because in general using reloadData() every now and then could be very resourceful depending on your cell layouts.

NSFetchedResultsController saving data but UITableView not configuring cell

When returning from a ViewController where I add and save an NSManagedObject, the TableView inserts a blank cell where the new cell should be. If I back out from the tableview screen and go back, it reloads and configures everything properly.
So what I know from this is that the data is being properly saved to the store, I know the FRC delegate operations are all being called (.Insert, for instance), and I actually can see via breakpoints that my "configureCell" method is being called and is passing back a fully configured cell. The tableview is just putting in a blank one instead.
I can post code if you'd like, but I was just curious if this description rang any bells for anyone? I've been researching this problem off and on for weeks but haven't really found anything similar.
For troubleshooting purposes I:
took my custom UITableViewCell out of the mix and just had the tableview configure a generic cell... didn't help.
created a stripped-down version of the app flow in another project, including core data, FRC, custom tableview cell, etc... this actually DOESN'T display the same behavior. I've compared the two projects side by side and can't determine any meaningful difference (other than the data itself that's being displayed).
I've even gone so far as to insert a delay between the context.save() operation and dismissing the VC where I create the object, just to see if it's some sort of race condition between when the object is created and when the table configures the cell. Needless to say, that did nothing.
Is this a bug anyone else has encountered? Anyone seen this behavior before?
Thanks in advance, sorry for the wall but this is something that Google, various subreddits, and so far StackOverflow has not been able to help me with.
Well after rephrasing my question I found this from back in May.
New table view cell shows as a blank cell
No solution there, but I reached out to him via twitter and his suggestion worked. Switching my size class back to any/any seems to have solved the problem, which means this might be an autolayout bug. It needs more testing but I think I've done everything that would demonstrate the issue and I think it's resolved.

UITableview cell reinitializing every time in iOS 7

All,
I hope most of you know that with ios7 there is not need to do a null check for tableview reuse
if (cell == nil) {
But unfortunately, because of that the cells are always reinitialized, as we put the code in the same method for initializing values. The problem is only with text fields inside the tableview though.
Let me explain the scenario. I have a table view with multiple rows, and some rows contain multiple text boxes. I populate the textboxes with data from server when the page is loaded. Since the cells are always re-initialized as i explained above, whatever I enter in the field goes away and the server data is re populated once i scroll down and come back to the initial stage. This is because the populating the data code is also in the same place. After fetching a reusable cell it populates the data.
Previously till ios6, we used if(cell==nil) and hence we loaded server data inside the cell and when reusing the cell, this piece of code will never be called.
I have other dirty solutions, but would like to know if someone else has a graceful way of dealing this. Please help.
You just don't store any data in the table view cell but in the model that fills the table cell. This is always the way it should be done.
Looking from the MVC standpoint than the UITableViewCell is a view. Since it is reused by iOS you should use a model to the view.
Yes, this is the expected behavior of UITableView. For performance reasons, cells are reused. Thus, it is your responsibility to populate the views in a Table View Cell every time tableView:cellForRowAtIndexPath: is called.
The thing I don't understand from your question - are you making a network call every single time a cell comes into view? If so, cache the results somewhere. Or, if it's a small amount of data, consider just doing it all in one shot at the beginning (still need to be asynchronous though).
One thing I see a lot of developers do is move a lot of code into UITableViewCell subclasses, which sounds like a good idea because it's modular, but makes solutions for problems like this more difficult. Have the Table View Data Source manage the network calls.
If you need some inspiration, look at Apple's LazyTableImages sample.

iOS Collection view cell update is delaying

I have sample collection view. I am dequeue cell from it by dequeueReusableCellWithReuseIdentifier:forIndexPath:
When ViewController is loaded the OS dequeue 5 cells and load information on it.
When I scroll, another 5-7 cells are dequeued and information is loaded on it.
The second set of loaded cells is coming in foreground with information from first set of loaded cells and the information in them is updated with fresh (right) one after less than a half second.
I am using prepareForReuse for cells, where I reset visibility of all cell components and than load fresh information, but the result is not what I expect, it is what I described above.
Any suggestions?
Thanks.
EDIT: Well if I update cell views with data from dequeueReusableCellWithReuseIdentifier:forIndexPath: all works fine, but if I pass the data to cell and ask cell to modify itself - than I have a problem pointed above.
EDIT 2: Soved. As I imagine - it was stupid mistake. As advice for others - make sure that you pass data to cell in any cases.
Do you have ALLOC statements in cellForItemAtIndexPath? or if using a subclass, do you have any ALLOC's in the subclass implementation? Possibly you seeing ui elements that are not being cleared out.

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!

Resources