How to improve UILabel performance in a table view? - ios

I have a table view with custom cells and I am trying to optimize it to make the scrolling as smooth as possible. I've followed most of the advises about table view optimization, for example, using fixed heights, not using AutoLayout etc.
I have achieved in a very optimized state except one thing. The custom cell has one image and several UILabels. With one image & 3 labels, the performance is perfect. However, when I add the 4th label, I start to observe a bit of jerkiness, very small but not as smooth as before.
I have searched the net and found CoreText can help performance. Is CoreText a right direction? Are there any other things that can help to improve UILabel performance when used in a table view?
EDIT:
I only use simple functions for UILabel by setting its font, font color, text and numberOfLines.

Core text is probably the ultimate solution for performance, but even before getting there I like to use a program called PaintCode to draw up the contents of my cells if they are particularly complicated.
Other things you can do however to speed things up a bit before you need to get to that point:
Do all calculations before cellForRow is called. So any number formatting, date calculations etc, should all be done and stored on your model object beforehand. That way you can just plug them straight in to your cells properties and they're ready to be displayed. Any extra time spent in cellForRow will slow down your tableview.
Set all labels and views to opaque. You'll probably have to set their background color when you do this, but it makes drawing the views that much faster. You can see which views have transparency applied by turning on "Color Blended Layers" from the debug menu for the simulator. What you want is to have your cells completely green.

Keep some dummy text in all labels by default. Updating text would not cost memory.
CoreText is pretty expensive and probably not meant for this! I think dummy text tricks should work for you.
Hope this works.

Related

Is it possible to use a UITableView's cells as a mask for a view underneath it?

I want the background image to only be visible where the individual cells are, so as you scroll it reveals different parts of a fixed image underneath. It's basically the opposite of a common tableview with opaque cells over a background, if that makes sense.
I still want the cells to be rendered normally in addition to their edge serving as a mask boundary, since they'd have text and other subviews that aren't just for masking.
I can't find an example, but I've seen this effect before done using CSS and it looks nice. It's not incredibly complicated, but I can't seem to figure out an efficient way to do it in UIKit.

Autolayout self-sizing cells and UITableView stuttering/jiggling

Learning efficient ways of calculating the height of dynamic tableview cells with autolayout. Faced the problem when scrolling table view, it begins to stutter a little bit, which is very annoying. Sometimes it stutters a lot when scrolling up, but that is probably related to my demo core data code.
This video demonstrates the problem. The sample project is on github repository (see SZNewsFeedViewController).
The layout of the cell is very simple:
My first guess was looking at blended layers in the simulator, but it's fine:
Stuttering does not go away if I comment out images setting, so, that is probably not the real reason. I set the images asynchronously with FastImageCache. I think I could watch for active scrolling and skip images setting until it stops completely, but I'm not sure if this could really help.
Replacing tableView:heightForRowAtIndexPath: code with return 250 does not solve the issue completely too.
Right now I change the height constraint constant for posts with images, will try a separate cells for plain text posts and images posts.
Can you point me out, what may cause such a stuttering and what can i try more to solve it? What's wrong with my layout or height calculating code?
Is there any appropriate way to pre-calculate heights (with the same values as systemLayoutSizeFittingSize returns) in the background thread and save it to core data model, to be able to reuse those values fast?
EDIT: the profiler say that height calculating takes most of the time:
UPD:
Ok, seems like a separate cells + height caching is a better solution - tableview jiggles less for a little bit. Also, tableView:estimatedHeightForRowAtIndexPath: seems to be more harmful then helpful. Removing this method removes jiggling a little bit more too. I figured out that after the approaches above, [collectionview reloadData] call affects most of all - removing this call leads to almost no jiggling. I will try static imageviews + watching the active scrolling to determine if this wouldn't lead to jiggling.
It looks like you're still implementing heightForRow:atIndexPath: and not utilizing UITableViewAutomaticDimension.

UITableView with floating cells

I am working on a specific type of list for an iOS app, where cells would simply have a specific width and height and then float left (think of something like CSS float:left), creating then a list that would scroll if more than 12 items (like the picture bellow).
I was thinking that perhaps this could be done by customizing UITableViewCell and setting the frame's width and height, so I could re-use the various benefits UITableViewController, delegate and dataSource offer. but unfortunately it seems that's not possible.
So, I'm now simply creating many UIViews and positioning them across its parent programatically.
I just would like to make sure this would be the right approach to achieve such kind of lists and if someone could let me know if it's indeed not possible to have this kind of functionality on UITableView and inherit all the goodness it offers.
Thanks guys
I know this question is already answered, but still...
I believe you can simply use UICollectionView since iOS 6.0.
Documentation
And this is how it looks like (image)
Maybe this will help somebody.
UITableView can only have cells that go in one direction. It's perfectly okay to create many UIViews (table view cells are also UIViews), if the number of them is reasonable. You can usually have a few hundred UIViews in a scrollview without seeing any performance issues, but that also depends on the complexity of what's inside the uiviews. Try it for yourself and if you find out, that you need too many uiviews, or that their complexity it too high, you can optimize further. You can set the .layer.shouldRasterize property to "cache" the content of the views. Or you can use CATiledLayer, which is something like a UITableView, but it supports tiles in all direction (and scrolling, zooming, ...).
Take a look at this project, I think may be helpful to for your problem
link

IOS Complex view

(please forgive my english)
I'm new at IOS developing but I've a good skill on other programming languages (mainly c#).
I'm trying to develop a test app with a main window.
Inside that window there is a scrollview with fixed size. inside that scrollviewer there is a view containing some stacked couples of one TextField and one Label: I can't know - ad design time - how many "rows" of them I have to put into.
My problems is:
I can put on the view the first couple (a textfield and, next, a label). Label size can be one or several rows high ,so I've to set multiline and sizeToFit
How can I put the second couple (and so on) without overlap the existing labels..? I can't use a table because between rows there are other labels (title of section).
Do I have to calculate the height of each label and programmatically calculate where to draw the next? Is there a more efficient way?
I don't need code, please just some hints or some keyword to googling on.
Many thanks.
I don't see why you can't use a UITableView, you can set up cells with all the controls you need in it. Either way, using a UITableview won't make the job any easier (just more memory efficient if you have many rows).
The only way that I see is, as you said, to calculate where the next 'row' should be placed (depending on the height of your previous rows). You'll also need to calculate the entire height, in order to set it to the contentSize of the UIScrollView.
Instead of UILabel you can also use a non editable UITextView, since it'll be easier to get its size (after you set the text, you can set the size of a UITextView to be equal to its contentSize)

Transparent custom UITableViewCell

I've finally finished my first large application, the only problem is that I've focued a lot on design, and I'm using custom nibs as cells with transparent backgrounds. When I tried testing the application on my iPhone, the performance was terrible.
Is there any way to get better scrolling performance while using transparent cells with a ImageView behind the UITableView?
I've read two articles mostly:
blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview/
cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
Looks good, but what if I want to use transparent cells?
a) Uses solid color.
b) Uses imageview as background.
Any help will be greatly appreciated. I want to get this baby released as soon as possible, but the performance as it is now is terrible!
First off, stop using nibs. Every time a cell is created, you're now hitting the disk in order to unarchive the nib. 3.1 will actually make this better, but until then, please create your cell in code.
Secondly, remove transparency wherever you can. Anything that doesn't need to be transparent, shouldn't be. And anything that isn't transparent should have the opaque property set to YES.
A third suggestion is if you're using a lot of subviews, you will see a performance benefit by using a custom view to draw everything instead of a bunch of subviews. If you choose to go this route, you should consider how it behaves when rotating to landscape mode (e.g. how the stretch action occurs), or if you have any controls that need to handle touches separately from the cell itself.

Resources