How can I speed up a UITableView?

前端 未结 9 1939
广开言路
广开言路 2020-12-22 23:17

I have a UITableView with about 400 cells in 200 sections and it\'s a little sluggish in responding to user interaction (scrolling, selecting cells.) I\'ve made sure the met

9条回答
  •  情书的邮戳
    2020-12-22 23:42

    Even if your cell is actually that simple (background image and label) there are some things to consider

    Image caching This is the obvious thing - if you are using the same image everywhere, load it once into the UIImage and reuse it. Even if the system will cache it on its own, directly using the already loaded one should never hurt.

    Fast calculation Another rather obvious thing - make calculating height and content as fast as possible. Don't do synchronous fetches (network calls, disk reads etc.).

    Alpha channel in image What's expensive when drawing is transparency. As your cell background has nothing behind it, make sure that you save your image without alpha channel. This saves a lot of processing.

    Transparent label The same holds true for the label on top of your background view, unfortunately making it opaque might ruin the looks of your cell - but it depends on the image.

    Custom cell In general, subclassing UITableViewCell and implementing drawRect: yourself is faster than building the subview hierarchy. You might make your image a class variable that all instances use. In drawRect: you'd draw the image and the text on top of it.

    Check compositing The simulator has a tool to highlight the parts that are render-expensive because of transparency (green is ok, red is alpha-blending). It can be found in the debug menu: "Color Blended Layers"

提交回复
热议问题