1

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Joe Huang
  • 6,296
  • 7
  • 48
  • 81
  • As state in the below article: - UILabels sould have their opaque properties set to YES - Use cell.layer.shouldRasterize = YES http://stackoverflow.com/questions/3248201/why-is-scrolling-performance-poor-for-custom-table-view-cells-having-uisegmented – Guy Daher Feb 09 '16 at 04:44
  • @GuyDaher tried but it seems not helping the performance – Joe Huang Feb 09 '16 at 04:46
  • pretty much guaranteed your issue is not the UILabel but the images in your cells. – Rog Feb 09 '16 at 05:24
  • @Rog when I have the images and one UILabel, the scrolling is in a perfect state. Only after I add more UILabels, the scrolling starts to get jerky. Maybe you are right, it might be other things but I am still like to learn if any thing I can do to enhance UILabel performance in a table view. – Joe Huang Feb 09 '16 at 08:41
  • Are you doing any heavy calculations in cellForRow? particularly for this new label. – Beau Nouvelle Feb 09 '16 at 08:45
  • @BeauNouvelle the text was fetched from CoreData and stored in an array of `NSManagedObject`. The code is simply like this: `newLabel.text = data[index].valueForKey("text")`... The scrolling performance is actually quite good & acceptable, but I just try to optimize it to the best possible case. – Joe Huang Feb 09 '16 at 09:04
  • Is this in the simulator? Have you tested it on an actual device? You should be able to have far more than 4 labels in there and an image if you're only loading content from a data store. Simulator performance can be bad at the best of times. Test on a device. – Beau Nouvelle Feb 09 '16 at 09:09
  • of course it's on devices. the quality is actually very good already, many famous apps cannot achieve what I have done, but I am looking for being perfect, so I try to understand all possibilities. – Joe Huang Feb 09 '16 at 10:57
  • The only other way to improve performance beyond what I've already told you is to draw the text and images in the cell yourself. You can't go much further than that. – Beau Nouvelle Feb 09 '16 at 21:15

2 Answers2

5

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:

  1. 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.
  2. 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.

enter image description here

Beau Nouvelle
  • 6,962
  • 3
  • 39
  • 54
  • I didn't set anything about opacity but I do see red areas, which is odd to me. So is the default not opaque for the labels and views? What do you mean to set them to opaque? (which property? ) – Joe Huang Feb 09 '16 at 07:39
  • I tried everything I know to set the UILabel to opaque (such as background color alpha to 1, or layer.opaque to 1, etc.), I still get a red area for the UILables. Could you let me know how to "set all labels and views to opaque" programatically? – Joe Huang Feb 09 '16 at 08:38
  • This question might help you? http://stackoverflow.com/questions/8520434/uiview-opaque-vs-alpha-vs-opacity – Beau Nouvelle Feb 09 '16 at 08:40
  • If you're still having issues, update your question with some screenshots of Blended Layers and I'll see if I can help from there. You can also set this opaque property on the label in Interface Builder. – Beau Nouvelle Feb 09 '16 at 08:44
  • Finally I found out it's not because I was doing it wrong but if the text is in Chinese, it's always in red (Apple adds another layer on it). I asked another question for this: http://stackoverflow.com/questions/35308501/is-it-possible-to-make-uilabel-with-non-english-characters-green-when-color-blen – Joe Huang Feb 10 '16 at 08:36
-2

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.

TechBee
  • 1,897
  • 4
  • 22
  • 46