How to optimize complex UITableViewCell

  • 1. The row height of the cell is not fixed. If you need to calculate the row height, cache the row height as much as possible to avoid calculating the row height repeatedly. Because heightForRowAtIndexPath: is the most frequently called method.

  • 2. Swipe and load on demand. This works great for large image displays and web loads! SDWebImage has implemented step-loading in conjunction with this performance bar.

  • 3. Use reuseIdentifier correctly to reuse Cells

  • 4. Use as few transparent layers as possible

  • 5. If the actual content in the Cell comes from the Web, use asynchronous loading to cache the request results

  • 6. Reduce the number of subviews

  • 7. Try not to use cellForRowAtIndexPath in heightForRowAtIndexPath:, if you need to use it, just use it once and cache the result

  • 8. All subviews are pre-created. If you do not need to display them, you can set hidden, and add views dynamically to the Cell as little as possible

  • 9. Don’t use ALpH for colors

  • 10. Rasterize

  • 11. Set the opaque value of the subViews of the cell to YES, and try not to include transparent sub-view opaque to assist the drawing system, indicating whether UIView is transparent. In opaque situations, render views need to be rendered quickly to improve 􏰀 performance. One of the slowest rendering operations is blending. The way to improve 􏰀 performance is to reduce the number of mixing operations, which is actually the improper use of the GPU, which is done by the hardware (mixing operations are performed by the GPU, because the hardware is used for mixing operations, not just mixing). The key to optimizing the hybrid operation is to balance the CPU and GPU load. ShouldRasterize of the cell layer should be set to YES.

  • 12. Cell asynchronously loads images and caches

  • 13. Asynchronous drawing

    • (1) When drawing strings, use drawAtPoint whenever possible: WithFont:, Instead of using more complex drawAtPoint:(CGPoint)point forWidth:(CGFloat)width withFont:(UIFont *)font lineBreakMode:(UILineBreakMode)lineBreakMode; If you want to draw a string that is too long, you are advised to cut it yourself and draw it using drawAtPoint: withFont: method.

    • (2) When drawing pictures, try to use drawAtPoint instead of drawInRect. DrawInRect can be particularly CPU intensive if images are scaled and shrunk during drawing.

    • (3) In fact, the fastest way to draw is if you don’t do any drawing. Sometimes through UIGraphicsBeginImageContextWithOptions () or CGBitmapContextCeate () to create the bitmap will show more meaningful, from a bitmap images captured above, and is set to the content of the CALayer. If you have to implement -drawRect:, and you have to draw a lot of things, that will take up time.

    • (4) If the image in the cell needs to be downloaded during cell drawing, you are advised to start the image download task after drawing the cell for a period of time. For example, draw a default image first, and then download the image of the cell 0.5 seconds later.

    • (5) Even if the cell picture is downloaded in the child thread, too many child threads should not be opened during the cell drawing process. Ideally, only one child thread is active for downloading the image. Otherwise, it will affect the drawing of the UITableViewCell, and thus the sliding speed of the UITableViewCell. (suggest combine NSOpeartion and NSOperationQueue to download the pictures, if you want to find the download pictures, can put the self. QueuesetMaxConcurrentOperationCount: 4)

    • (6) It is better to write a cache to cache the UITableViewCell in a UITableView, so that a cell can be drawn only once in the entire life of the UITableView, and if memory runs out, It also effectively frees the cached cell.

  • 14. Do not set the tableView’s background color to an image. This seriously affects the sliding speed of the UITableView. In a free search for a limited time, I’ve turned over a mistake: the self tableView_. BackgroundColor = [UIColorcolorWithPatternImage: [UIImageimageNamed: @ “background. PNG]]. Setting the background color of a UITableView in this way seriously affects the sliding smoothness of the UTIableView. Change to self.tableView_.backgroundColor = [UIColor clearColor]; After that, FPS went up from 43 to around 60. It slides smoothly.