Document directory
- The solution
- Why it's fast
- The Code
Http://blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview/
Fast scrolling in Tweetie with uitableview
December 12,200 8
Scrolling is the primary method of interaction on the iPhone. It has to be fast. ItHasBe fast. More than a few developers have asked me how I do it in Tweetie, so I figured I wocould share a really fast and really clean technique people can adopt in their own apps.
The solution
Cutting to the chase, here's the secret: one custom view per table cell, and do your own drawing. Sounds simple? That's because it is. it's actually simpler than dealing with a ton of subviews of labels and images, and it's about a bzillion times faster (according to my informal tests ).
Why it's fast
Much like on Mac OS X, there are two Drawing Systems on the iPhone. one is CoreGraphics, the other is layerkit coreanimation. coreGraphics does drawing on the CPU, coreanimation does drawing on whatever it thinks is fastest-most likely the GPU.
The GPU on the iPhone hates blending, That's why Apple recommends that you keep as your of your views opaque as possible. sometimes you have no choice-if you have a label over an image you are forced to make the label transparent otherwise you get a big uugly block around your text.
What's a developer to do? Pre-blend of course... With CoreGraphics into your own view. if you blend your stuff together into a single static view on demand (e.g. when a table view moves a cell onscreen), it's a little bit more expensive for the first frame, but every frame after that coreanimation is just dealing with one big, opaque texture... Which itLoves. It's more than just the blending too. if you think about what is happening in terms of overdraw, having one big view per table cell is a big win because coreanimation will only touch a single given pixel on the screen once rather than multiple times (potentially, depending on how much overlap your old view hierarchy had ).
The Code
I put together a small example project showing off this technique. it's a simple scrolling list of cells with some text but draws the text using two different fonts, much like the address book contacts list on the iPhone (regular and bold fonts ). the technique is extensible to pretty much any style cell you need-I use the same thing in Tweetie and draw the chat bubble, text, and Avatar all together into a single view.
There is one handy class that you can use for everything, see:Abtableviewcell. hAndAbtableviewcell. mIn the full example project here: fastscrolling.zip.
It doesn't help that Apple doesn't have any good examples on how to get good scrolling performance. the one table View Example I checked (a while ago, it may have been updated) was absolutely horrible in terms of scrolling performance. horrible.
If you wanted to know why Tweetie is so fast, now you do. It's an incredibly simple technique and shocould be very easy to adopt.
Update:It looks like Apple has in fact updated the example code. Check out the same th example in their tableviewsuite.