iOS Development Series-Nine lattice algorithm-xib

Source: Internet
Author: User

To show you the application download Small project,: involving knowledge points: lazy loading, nine Gongge algorithm, dictionary to model, custom UIView, xib file usage

First drag the footage into the Xcode project: Simply take a look at the footage file

At this point we should first pay attention to. plist suffix files, which are stored in the data we use, and then look at the data in the Plist file

From here you can see that there are 12 applications downloaded, that is, 12 uiview controls,

Next, we load the plist file and use the data inside it. First, instead of dragging the plist file into Xcode, it automatically uses the Plist file to load the data inside it.

Because the Plist file root node is a nsarray type, we create an array property that holds the data from the plist file

After the array property is created, the plist file is loaded. That is, linking plist with apps, which means loading data

So when do you need to load the data? This time it involves (lazy loading) knowledge points

Lazy loading: When to need data, when to load, if you do not need data, I do not load data, it seems lazy, so called lazy loading, how to implement lazy loading it?

Analysis: As long as you know when to use the data, then I know when to load the data. We usually use the dot syntax when using the data (getter method) such as Self.apps

This is called the Getter method, which gets the data inside the apps array. Program, We must have a lot of places to use Self.apps, the first time we use Self.apps point syntax, we need to load plist file, when the second use of Self.apps, when the application has already had the data in the Plist file, we do not have to re-load, although said loading Will not be a problem, but it can affect the performance of the program. If this analysis, we can not do something in the Getter method, if there is no data to load, no data, we go to load it? Lazy-Loading implementations:

This time the apps array is stored in 12 dictionaries, but the dictionary has a lot of problems and inconvenient to write in the later use, then we involve the dictionary to the model

What is the conversion of dictionaries in apps into model models? A model is a class that inherits NSObject, a member property in the class that holds the data in the dictionary

So when the dictionary goes to the model, the apps store the model object, not the dictionary.

The key point of the dictionary-to-model is to convert the stored information into the value of the member property of the Model object.

First create a class, this class has no special requirements, as long as the inheritance of NSObject on the line

This class should have member properties, which are set according to the key-value pairs in the dictionary: a key-value pair is a member attribute in the corresponding class, so the class has two member properties. Determine a property: Type, property name, type determination: The type depends on the type of value in the key-value pair, and the property name depends on the key value.

The Appsmodel model class has been created, so long as the data in the dictionary is assigned to the member property in the model, then the model object is added to the array and passed to _apps, when the model object is stored in the apps array, which is the so-called dictionary-to-model. Implementing dictionary-to-model in lazy loading

Model Code Optimization:

Next, we implement a quick Create object method in the model class, first declaring the method in the AppsModel.h file

Implementing methods in the APPSMODEL.M file

At this point, lazy loading in the dictionary to model can be written like this:

Well, so far, we've self.apps the model data stored in the array. The data loading work is done, so let's build the interface.

Analysis:

There are 12 app download icons on the interface, how do 12 numbers come from? Is the length of the array Self.apps. That is, there are several models stored in Self.apps. In the final analysis, obtained from the plist file.

Re-analysis: the size of the 12 app download icons is the same, the format is the same, that is, each application download icon (UIView) is composed of pictures (ImageView), name (lable), download button (button), in addition to the picture and the name of the data display is not the same , everything else is the same. At this time introduced nine lattice algorithm, first build 12 small UIView display on the interface.

The essence of the nine Gongge algorithm is for loop, because everyone sets the data is not the same, I take my example, the main thing is to know how each control's X, y value is calculated, X value to use the I%3,y value to use I/3, you know this can be

To do this, we already have a rough outline, and this time just add a control to each red UIView. Just analyzed, each small uiview inside has 3 controls, this time adds, we need to use the code to set the frame, each control's property, is troublesome, the code is cumbersome.

The following describes the creation and use of xib files, what xib is used for, Xib is used to describe the interface of some screens, and storyboard is used to describe the entire screen interface

Why to propose xib, if we use xib to describe each small uiview, then we put Xib described UIView directly into the Self.view, very simple and convenient. This time also do not need to set the frame of each UIView child control, the property is set directly in the property area, very simple, very convenient

Description: Creation of the Xib file

Now that the Xib file is created, the Xib file already exists; open Xib file,

We can add any control to this:

So, let's assign a value to the control and assign the data inside the Self.apps to the corresponding control.

To assign a value to a control, you get the control first, how do you get the control? Before we say, Xib file and storyboard very much like, we see how storyboard do,

We usually take a drag line to the control, in the VIEWCONTROLLER.M to get the control. Then storyboard is how to connect with Viewcontroller, drag line must meet certain conditions to drag, or can not be dragged. So what are the conditions that storyboard and Viewcontroller meet? Here's a look at Viewcontroller,

In fact, Viewcontroller is only a class, but inherits from Uiviewcontroller. Why do you inherit Uiviewcontroller? Let's take a look at storyboard.

The Viewcontroller is the outermost, and the rest is its child controls. Xcode sets itself up with a Viewcontroller class that inherits Uiviewcontroller and then sets Viewcontroller to the real class of Viewcontroller. The storyboard and the Viewcontroller class are linked together. So we mimic storyboard, set xib

Just now we said, Xib outermost is uiview, so this time we create a class, as long as this class inherits UIView, it can be

Then modify the actual type of UIView:

At this time Appsview is the real class of UIView.

This time we'll be able to tow the line.

Well, we've got the control in the appsview.m file, where the data is inside the Self.apps, Self.apps the model object stored inside the array, so we set a model property for Appsview

Why do we have to set this property? Apps is a property in the Viewcontroller class, so I get the properties of other classes in the Appsview class? This time, we can put the Viewcontroller class in

The Apps property is passed as a parameter to the Appsview class, and then I receive it by setting a property in the Appsview class. This time I set the property type in the Appsview class to be the same as the type of data being received, so we're going to set an object of type Appsmodel.

Now, the Appsview class has a Appsmodel type of App member property,

At this point, we can get the data in the Appsview class, so our initial goal is to add data to the control, not to add data to the app properties, so what's next?

Analysis: Appsview.app = self.apps[i] This line of code, point syntax this time is setter, that is [Appsview Setapp:self.apps[i]], this time is called the property app setter method

Now I'm writing this code. The final goal is to add data to the control, and the control's member property is

We finally assign values to ImageView and namelable, Appsview.app = self.apps[i] This code calls the app setter method, and we can assign values to these control properties in the setter method

is to override the setter method:

After the setter method rewrite is finished, we re-analyze Appsview.app = Self.apps[i] This time self.apps[i] is equivalent to the parameter, the data is passed to the control property.

Run the program to see the results

The program is over!

iOS Development Series-Nine lattice algorithm-xib

Related Article

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.