UITableView Performance Optimization 10 tips

Source: Internet
Author: User

Usually you will find a picture class app that does the following on a imageview:

1 Download picture (main content picture + user avatar picture)
2 Update timestamp
3 Show comments
4 Calculating the height of the dynamic cell

Tip#1 Learn how to improve speed

1. Open your project and click Product>profile

2. Choose Custom From there

3. Find the Add button and add Tool: Allocations,time,profile,leaks

4. Observe your application, and his performance.

Tip#2 Avoid blocking the main thread in this example, you will see that the first image-related method blocks the main thread when the data is downloaded and converted into a picture object. You should try to avoid blocking the main thread, which is especially important for interacting objects in collection. Network requests? Keep them running in the background (asynchronously) and the cache returns the response. You certainly don't want to repeat any operations. Imagine your cell being drawn within a period of silence. Your cell should show only the data that has been saved on your device. It will make you feel better. Tip#3 Reusing cells

If you've spent some time learning iOS, then I'm sorry. This advice is for those who have new contacts with iOS. You should use the Dequeuereusablecellwithidentifier method to get a cell above the table or collection.  If you don't do this, you're wasting a meaningless amount of time and data. Tip#4 Cache Downloaded Pictures

This is certainly the most important piece of advice you have read here. If you do not cache pictures you will encounter a lot of problems.

If you reuse a local image then use the UIImage method imagenamed:. Requesting pictures in JPG format will save time and resources. If you are getting images from the server, then you can get the pictures you need (if you ' re getting your image from a server, you have the luxury of sending the exact image th At ' s needed.). PNG files occupy a large portion of the space in memory. If you're curious about this, you can convert JPG to PNG in the example to download a series of PNG images.

Use Sdwebimage or Heneke to manage your pictures. In the example provided, I was using Heneke, and I haven't heard of it before.

Tip#5 the cost of using rich text tags is very expensive

It's too expensive to use rich text tags. Avoid using this as much as you can. Ask yourself if you really need this. If so, do the caching as much as possible. Tip#6 Cell Height Calculation

If your table has a complex dynamic height then you need to cache the height of the calculation. Consider how often it is calculated (especially for collection views), and you want these heights to be directly available. The pain of tip#7 NSDateFormatter

Just like rich text, if you initialize frequently, date formatter can cause a lot of memory consumption. Ideally, your Web side will provide you with readable text (much easier to calculate than in the last time). If not, you can create a nsdateformatter singleton to use. NSDateFormatter is not thread-safe, but iOS7 and beyond are no longer the case. Thank Quellish for reminding me of this. Tip#8 Transparency

If you can avoid it, the object you create is best opaque (opaque, you can't look through it). If you have a transparent picture, the system needs to work hard to redraw the pictures. You can actually see these areas in the simulator by clicking Debug>clolor blended areas. See the red, that means these areas are transparent. It will be very time consuming when you are dealing with a collectionview. Ideally, you want to see the whole screen is green. It may not be feasible for your design, but try to reduce the amount of red you see. In the example you can see that the label extends to the end of the view and can be erased. Tip#9 don't use xib too much (use storyboard if you can)

Be careful if you want to use Xib. When you load a xib, the entire content will be loaded into memory (images, hidden views). But this doesn't happen in storyboard. He only instantiates what is currently being used.

There are some special scenarios where using xib is very meaningful. For example, you might want to use some third-party frameworks and they write collection UI parts in a purely code-based way. If you want to use Xib to create a prototype cell you can do it with xib. Just be careful not to overload. tip#10 use coregraphics I seldom need this, but you can use it when you need it. Use Coregraphics and write your UI code in a view's DrawRect method.

Challenge

Who doesn't like a good challenge? Let's keep it down here. Take the time to do the next two things this week.

Learn how to use instruments (time Profiler, allocations)
Check the usage of learning instruments by testing the picture cache (Turn it off, open, watch).
Https://github.com/mcgraw/dojo-table-performance

UITableView Performance Optimization 10 tips

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.