iOS Development > Learning-10 tips for accelerating table views development

Source: Internet
Author: User

English: David McGraw

Translator: Cocoachina Translator yake_099

Website: http://www.cocoachina.com/ios/20150729/12795.html

Click "Read original" To view this page

Before we begin, I am prepared to listen to an opinion more than this year. Please take some time to give us some feedback through this brief survey. This will help me to help you.

If you have ever dealt with CollectionView, you may have realized the value of this article. If you don't pay attention to the speed this will be a big problem and your users will let you know. You will soon realize that your scrollview is fast without the other apps on your device. Table views are the first to be used by every iOS junior developer, and may soon be confused. This article will take a closer look at some of the issues you may be looking for.

Turtle and rabbit Problems

Table Views is an interactive object that many apps use to showcase structured data. It is trivial to use it very well, which makes them use it like a tortuous adventure. Designers do not consider the problem of performance at the beginning of the design. The designer may even be your own. Soon you'll be making a picture-type app that needs to show a lot of information on the cell. It may be fast at first, but soon it will be as slow as a turtle. You want to make your table views as smooth as a piece of butter. These effects of your app will soon be noticed if it's not good.

Speed up your table views

We will explore these tips in a practical example, and the table views in this example are not very well implemented.

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

    • Download picture (main content picture + user avatar picture)

    • Update Time stamp

    • Show comments

    • Calculate the height of a dynamic cell

In this example, we intend to focus on the above points.

I suggest you clone the demo directory (GitHub) to experience how bad it was at first. Jump to Xmcfeedtableviewcell to see its ascension and feel its performance. If you're running on the iphone 6+ it might not be so good to be aware of it and it's important to realize it. Don't forget to try it on an older device.

Tip#1 Learn how to improve speed

I can write an entire article about instruments. Here I will give you a general introduction because it will be very helpful.

If you are not very experienced with instruments, I advise you to spend some time on weekends to study some. When you want to measure memory and time consumption, they will help you a great favor. However, when you start to make an app you will encounter a lot of problems in the development process, the code will become worse, you may not have time to consider the performance of the problem. But refactoring is a potential. For proper refactoring you should spend your energy on analyzing performance.

So, here's what the weekend explores:

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.

For example, we are concerned with speed (but memory is also a big problem). Which tool do we need? If you choose Time profile then you are right. Let's open it and watch the app in motion.

Below you will find an overview of our app. All you see is that I open the app and scroll TableView as fast as I can. This simulates a good "worst case hypothesis" and then we can take action.

This area is the code that will execute when I start scrolling through the app, and we just want to know the time spent in this area.

Now you can start studying the code we discussed above. Double-click any row in these rows (preferably the top row, which is where time is consumed most)

It is important to point out that the option under Call Tree is not set for you when the instruments is loaded. You need to set it yourself.

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. I used the Heneke in the example provided, and I haven't heard of it before and I haven't heard of it.

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 using 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

Questions and Answers

There's no problem! Please leave a comment or send it to [email protected].

Takeaway

The interaction of the interface in iOS is important. This is not a compromise. If you don't take the time to stabilize your app's experience on the device, people may be far away from you. The app should be smooth when I look back at the content in the app.

iOS Development > Learning-10 tips for accelerating table views development

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.