tableview smooth scroll best practices swift - ios

It's a technical question, or about good practices...
I have this App, its a social network, where we have the timeline. In this timeline I have to render a lot of cells on a tableview which I have done this way:
create a Cell (with header, body and footer) -xib file
in this cell I have a method: setupCell() - which configures the contents
for each kind of post I instantiate a correctly view(xib) on body of this cell (like PhotoPost, TextPost or VideoPost etc) and configure constraint to set the size of views.
Also, I'm using:
tableView.estimatedRowHeight = 603
tableView.rowHeight = UITableViewAutomaticDimension
And kingfisher to download images asynchronous.
And, what is best way: storyboard, xib, or code?
So, my problem is that my scroll is lagging - I'm using reusable cells, but every time that tableview delegate calls cellForRow, I have to setupCell().
My first idea:
I get the post array with a task then a create cells for each post and append this to an array of cells, so when cellForRow is called I just get the right cell from this array.. It make better. But still not enough(I'm testing on a iPhone 5c).
My next ideia is create different cell for each kind of post, and save a variable for this cell of his size to setup on heighForCell method.
You think that looks good solution? Can anyone give me an opinion and suggestion? I'am very tankful in advance..

After experimenting with possible solutions I decided to use AsyncDisplayKit to solve my problem, it offers great smooth scrolling.
http://asyncdisplaykit.org
It's complete SDK and has well written documentation so was easy to implement. I would recommend it if you are struggling with a similar issue to me.

Related

Looking for the best scroll-view solution

I want to learn and implement the most suitable/simple solution to display dynamic data (JSON) in three lines and an active (clickable) download icon. Screenshot is attached
I would be glad to get your ideas and advice!
Thanks
UITableView is the way to go, as you can reuse a custom cell you define. And to add to that, UITableView's cells are loaded lazily and are reused. You can use a UIScrollView but if you have a lot of rows it can horde your memory real fast. In your case, a UIScrollView can be used as well, but if you plan on expanding, UITableViews would be the choice. All in all, go with a UITableView + a custom UITableViewCell
If you have array of data like this, the best way is using UITableView class. With UITableview, you can custom your UITableViewCell on the fly

Preload tableView cells and prevent reloading

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.

UICollectionview Scrolling while adding Items is super laggy

I have this very nice collectionview that loads pictures from my server. Now, if the pictures are loading slowly, the collectionview is actually scrollable, but if the pictures are loading fast, instead, the collectionview is basically frozen until all the cells are finally loaded.
I'm pretty sure this is something related to the fact that I'm using dispatch_async to update the ui (inserting cells and sections), so I'm wondering if there's a way to update the ui that still allows me to scroll the collectionview while the items are being added.
Is there anything I'm missing?
Thanks in advance for your help!
The easiest way to solve this issue is to use SDWebImage an UIImageView alternative , and call the sd_setImageWithURL:placeholderImage: method from the tableView:cellForRowAtIndexPath: UITableViewDataSource method. Everything will be handled for you, from async downloads to caching management.
You can find it here: https://github.com/rs/SDWebImage
So remove any dispatch_async, set right away the number of cells on this method:
- (NSInteger)numberOfItemsInSection:(NSInteger)section;
Scroll performance is going to be good with this tool

UITableView is getting stuck when loading data from UICollectionviewcell

I have a Uitableview in which i am loading custom uitableviewcells
and all that cells are calling a class which draw a collectionview
with scroll in it depending on amount of data,
so I'have seen that when I scroll then my UITableview take a jam on
those cells which are adopting scrollview then after a very narrow
second everything is fine but when I scroll again then it again
happen
I know that the problem is I am drawing cells on each time but I have
also take a look by initiating an array of views and then pass the
specific view to the cell on run time but strangely it got more stuck
when I am doing this.
Kindly help me on this
My Approach in your scenario would be to create “Section Header” and “Section Footer” for UICollectionView like the approach in this tutorial http://www.appcoda.com/supplementary-view-uicollectionview-flow-layout/ which i feel much simpler and easy to implement.

iOS Design implementation recommandations

I would like to know how to handle, for my iOS app, the following situation:
I have to deal with something like this (basicly this is an article with comments, received from a server):
=- Text and Images -=
=- UIWebView -=
=- List of Comments -=
Now, I came up with two solutions:
Have the content above the list of comments wrapped in a UIScrollView, and create Views for each and everyone of the comments (don't know many of them could be), and
Make the list of comments a UITableView, and the above content its Header.
Which of these (or possibly another if you have any recommandations) should I choose? It may look not very important, but I would like to know this, so I could use the idea in further developing.
I would use a UITableView with a custom UITableViewCell wich holds the comments.
It will definitely have a better performance the UITableView than the UIScrollView since the UITableView re-uses the cells.
Using a UITableView you will just have to worry about customising the cells for the comments.
Otherwise if you want to use the UIScrollView in case you have a lot of comments you will have to create manually a way to reuse them which is what the UITableView does.
If you want something like facebook, so that the context + comments both are movable,then go with option 2.
But, if you want content always at the top, go with option 1.
Indeed, UITableView is a subclass of UIScrollView.
And If you think you might have many comments like (50+),
If you use scrollView, then you should have to supply scrollView with those number of UIView objects.
But, if you use tableView, it perfectly reused already created views.So evenhough you have 1000+ comments, it just uses 5 UIView objects

Resources