Some summary of iOS

Source: Internet
Author: User

1?? Dictionary Turn model

A) There is a case of a model

1, see if the root node of the plist file is a nsarray type;

2, if it is: according to the dictionary elements in the file, create a corresponding model class, the property in the class is every key value in the dictionary, the type is the type of the key value (the model class's properties must be consistent with the key value in the dictionary, otherwise using KVC error).

3, customize the Init method of a class, the method is an object method,

eg:-(Instancetype) initwithdict: (Nsdictionary *) dict

{

if (self = [super init])

{

[Self calls the method of KVC];

}

return self;

}

Defines a class method that is used to convert a dictionary to a model object

Eg: + (instancetype) class name withdict: (nsdictionary *) dict

{

return [[Self alloc] initwithdict:dict];

}

So far, the model has succeeded in creating the image.

☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆ lazy Loading mode (key, must master) ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆

1, in the controller, create a property, type Nsarray or Nsmutablearray type (depending on the case, if you need to add the model object in the array later, create a mutable array, if you do not need to add the model to the array, create an immutable group), Used to store model data. (the array is called "Data Array" below)

2. Load data in lazy loading mode

The essence of loading data is to rewrite the getter method of the data array, which is what the Getter method writes:

① first determine if there is data in the data array, if there is no data to load the data, if there is no data to load data, directly return the data array.

② the process of loading data:

i) load the plist file first, using an array of "Nsarray *dictarray" to receive the data returned from the loaded plist file (why is it received in an array?). Because the root node of the plist file is an array type, the object that is stored in the Dictarray array is the Dictionary object.

II) The object that iterates through "Dictarray" with the for-in loop is a dictionary, which transforms the dictionary into a model object in the loop using the "Dictionary model class" created;

III) dictionary-converted model objects need to be stored with a mutable array, then turn back to create a mutable array "Nsmutablearray *modelarray", and then add the transformed model to the variable array "Modelarray" in the for-in loop. (You do not need to create a mutable array at the beginning, when you need to find that there is not a mutable array can be used to create a mutable array, this is what I want to say to create what the idea, do not follow the teacher's code from scratch to the end, so without their own ideas in the program, this program is not yours).

IIII) Assign "Modelarray" to "Data array"

IIIII) return "Data array"

Lazy Load Data Instance program:

-(Nsarray *) Modelarray

{

if (_modelarray = = nil)

{

Nsarray *dictarray = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle] pathforresource:filename OfType:nil]];

Nsmutablearray *modelarray = [Nsmutablearray array];

For (Nsdictionary *dict in Dictarray)

{

Model class *model = [model Class model class Withdict:dict];

[Modelarray Addobject:model];

}

_modelarray = Modelarray

}

return _modelarray;

}

II) Two models (case of nested dictionaries in a dictionary in the plist file) "Can see the sixth Day auto show model nesting"

"Attention" in such a situation, do not panic, this is more simple. It can be easily done in one step.

1, first first look at nested in the innermost dictionary, and then according to a model of the case, create a class to describe the innermost dictionary object, which is called the nested most inside the model known as the "small model";

2, then the same reason, according to a model, and then create a model, type and the outer dictionary of the key value of the same type, where the outer model is called "large model";

3, to this end, nested two models created successfully, simple.

4 "Note", when creating the model, be sure to start from the "small model", because in the "big model" to use the "small model", but also need to "note" is that the "small model" described by the dictionary needs in the "big model" in the initialization method to "small model"

For details, see the following "Sample programs":

"Small Model"

declaring files

#import <Foundation/Foundation.h>

@interface Lxlcarsmodel:nsobject

@property (nonatomic, copy) NSString *name;

@property (nonatomic, copy) NSString *icon;

-(Instancetype) Initwithdict: (Nsdictionary *) dict;

+ (Instancetype) carmodelwithdict: (Nsdictionary *) dict;

@end

Implementation file

#import "LXLCarsModel.h"

@implementation Lxlcarsmodel

-(Instancetype) Initwithdict: (Nsdictionary *) dict {

if (self = [super init]) {

[Self setvaluesforkeyswithdictionary:dict];

}

return self;

}

+ (Instancetype) carmodelwithdict: (Nsdictionary *) dict {

return [[Self alloc] initwithdict:dict];

}

@end

"Big Model"

declaring files

#import <Foundation/Foundation.h>

#import "LXLCarsModel.h"

@interface Lxlgroupsmodel:nsobject

@property (nonatomic, strong) Nsarray *cars;

@property (nonatomic, copy) NSString *title;

-(Instancetype) Initwithdict: (Nsdictionary *) dict;

+ (Instancetype) groupwithdict: (Nsdictionary *) dict;

@end

Implementation file

#import "LXLGroupsModel.h"

@implementation Lxlgroupsmodel

-(Instancetype) Initwithdict: (Nsdictionary *) dict {

if (self = [super init]) {

[Self setvaluesforkeyswithdictionary:dict];

Nsmutablearray *cars = [Nsmutablearray array];

For (Nsdictionary *dict in Self.cars) {

Lxlcarsmodel *car = [Lxlcarsmodel carmodelwithdict:dict];

[Cars Addobject:car];

}

Self.cars = cars;

}

return self;

}

+ (Instancetype) groupwithdict: (Nsdictionary *) dict {

return [[Self alloc] initwithdict:dict];

}

@end

"Attention" "attention" "attention" (important thing to say three times??) Note how the initialization method in the large model is written, "Why write this?" "" Because in the controller does not go directly to use the small model, he uses the big model, if needs to use the small model also uses the big model indirect use, (this is I to say to you who the matter who does, or lets the person to do) "

Please look at the following lazy-loading notation:

-(Nsarray *) Grouparray {

if (_grouparray = = nil) {

Nsarray *array = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle] pathforresource:@ "Cars_total.plist" OfType: Nil]];

Nsmutablearray *temparray = [Nsmutablearray array];

For (nsdictionary *dict in array) {

Lxlgroupsmodel *group = [Lxlgroupsmodel groupwithdict:dict];

[Temparray Addobject:group];

}

_grouparray = Temparray;

}

return _grouparray;

}

It can be found that lazy loading is exactly the same as the lazy loading of a model, "Why is that?" Because the small model is processed in the large model, it does not need to be processed by the outside world, it is also said that the inner nested dictionary is automatically converted into a model when the internal initialization of the large model, when the outside lazy loading only need to convert the dictionary of large model to a large model can be.

The discussion of dictionary-to-model and lazy-loading can be over, if there is something wrong or can be added to the welcome criticism.

2?? Proxy mode

The condition that the agent exists, ① the object ② proxy object ③ the protocol stipulated by the proxy object

Three things that are done by the proxy object: ① stipulation Agreement

② contains a proxy attribute (requires ID type and follows the proxy protocol)

③ has an event that can trigger an agent event in the protocol (that is, the proxy method that invokes the Proxy property in a method)

Three things a proxy object does: ① follows the protocol

② Setting Proxy objects

③ methods in implementing the Protocol

"Note" If there is an incorrect place to say, then correct it.

The places we have learned to use agents are: ①uiscrollview

②uitableview

③uialertview

④uitextfiled

"I think of these four for the time being, if you have any, give me a talk, learn together."

Add: Generally in the controller will be the proxy object is set to the controller itself, is also self,

For example: Self.tableView.delegate = self;

This means: Set the proxy object for the TableView control in the current controller to be the current controller (self)

Add a little (don't say I wordy): in the writing of the Protocol, it is generally necessary to the proxy object as a parameter passed in, if the method also requires additional parameters, then continue to declare the parameters after the method. "Don't ask me why I have to take the proxy object as a parameter, because this is the norm, that's what Apple is saying, and you can recall that all of the proxy methods you've implemented have the case of having the proxy object as a parameter." "Although I don't want to tell you why, but I still can't help but want to tell you my understanding, because this can call the method of the proxy object when needed, or can get the property of the proxy object".

"Last but not least", when do we need to use an agent to do things? When in their own class is difficult to achieve a thing, or can be achieved is not the current class of "responsibility" within the scope, this time you can use the agent, as to choose who to do my agent, this moment need to consider, who will do this matter please more convenient, or say this thing is who's "responsibility" where, This time you can find it to act as an agent, this time you need to declare the protocol in their own class, increase the agent properties.

"Please forgive my nagging, I would like to add a little more." When declaring the agreement, the agreement name is usually "followed by delegate" in the form of "class name",

The declared proxy attribute is @property (nonatomic, weak) id< follows the Protocol > delegate;

"Attention" "attention" "attention" "important things say three times??" The delegate attribute must be decorated with "weak" and cannot be "strong", otherwise it will cause a circular reference problem.

"Finally, it's really the last point." The agent is a one-to-many relationship, and the notification that needs to be discussed later is a many-to-much relationship.

Well, said here about the proxy mode of the shallow discussion to the end of it, is not a kind of impulse to kick my death.

If there is a wrong or inappropriate place, welcome criticism, if there is a need to add to the place, also hope to point out, we come together to learn.

3?? A brief discussion of data sources for UITableView

1, want the controller to become TableView data source, first let the controller follow the TableView data source protocol

<UITableViewDataSource>

Then set the TableView data source in the-(void) Viewdidload method: Self.tableView.dataSource = self;

(Of course, you can also use the method to set up the data source, you can also set up a proxy, not to repeat here).

2, it is time to implement the method in the data source protocol to set the data for TableView. The methods that must be implemented are:

@required

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section;

Row display. Implementers should *always* try to reuse cells by setting each cell's reuseidentifier and querying for available reusable Cells with Dequeuereusablecellwithidentifier:

Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing Co Ntrols)

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath;

Optional implementation of the method is more, the current we use is:

-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView; Default is 1 if not implemented

TableView data setup is very simple, just follow the correct syntax and operation, you can successfully let TableView show you want to display the data.

Some other properties of the settings, here will not repeat, if there is forgotten, you can read the teacher's code, or consult the relevant information.

"I'm going to say it again."//Implement the Proxy method to set the row height

-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath

{

Lxlqqchatcellframemodel *qqchatcellframemodel = Self.framearray[indexpath.row];

return qqchatcellframemodel.rowheight;

}

The above agent method, is set TableView line high, want to implement this method, you need to let the controller follow <UITableViewDelegate> Proxy protocol, I also want to say here, is < The uitableviewdelegate> protocol follows the <UIScrollViewDelegate> protocol, and if you want to implement some tableview drag-and-drop methods, you need to <uiscrollviewdelegate > query in the agreement.

"Finally, add a bit" the cell reuse method is needed to add cells to each line. Not very good description, directly on the code

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {

static NSString *id = @ "TG";

Lxltgcell *cell = [TableView dequeuereusablecellwithidentifier:id];

if (cell = = nil) {

cell = [Lxltgcell Getcell];

}

Get a model

Lxltgsmodel *model = Self.modelarray[indexpath.row];

Cell.model = model;

return cell;

}

① first declares a re-use flag, which is statically typed, in order to not repeat the allocation of memory for this variable, extending the declaration period of the variable. "Why do you do this?" Because the above method is called repeatedly, it is not necessary to allocate memory for the declared reuse flags every time.

② calls the [TableView Dequeuereusablecellwithidentifier:id] method to reuse the buffer pool to see if the hand has a cell that can be reused, returns if any, and returns nil;

③ determines if the cell is empty and initializes a cell if it is empty;

④ assigning values to individual controls in the cell

⑤ return cell

The five-part set of values for the cell was born??

Here, some simple discussion about UITableView's data sources is over.

Or that sentence, if it is inappropriate or wrong place, please criticize correct, there is a need to add the place is also very welcome to put forward, learn together.

4?? A simple discussion of customizing cells

This part of the discussion about the custom cell, I think the most interesting part of the teacher so-called the most difficult part, I think this part as long as the master method, in fact, he is very simple.   Why it's interesting to have a custom cell, because we can show what we want to display in the cell, based on what we need. Well, don't pretend to be forced, please forgive me. Next we get to the point.

A) custom cell via Xib file

When the cell's style is fixed, but the system does not meet our requirements, we can choose to customize the cell by means of xib.

Step: 1, create an empty Xib file, and then drag into a UITableViewCell control;

2, according to the requirements in the UITableViewCell to drag into the required control, and then set the relevant properties, where you need to emphasize the setting of cell reuse flag.

3, create a class to describe the UITableViewCell we just created, "note" The class created needs to inherit UITableViewCell this class

4. How can I associate a description class with a cell that was just customized? "Select UITableViewCell in the Xib file, and then in the Property Inspector window, modify the class to the UITableViewCell that you just created."

5. Add the properties of a model object to the description class, and then override the setter method of the model object to assign values to the individual controls in the custom cell in the setter method.

6, if the ability can be achieved, the steps to obtain a custom cell are encapsulated directly in the class method with the same name as the description class, see the code

+ (Instancetype) Getcell {

Loads the custom xib file, and returns

Lxltgcell *cell = [[[NSBundle Mainbundle] loadnibnamed:@ "Lxltgcell" Owner:nil Options:nil] lastObject];

return cell;

}

7. Then it is simple to set up a data source for TableView and a few simple ways to implement the data source protocol. For more information, see the discussion section on UITableView data sources above

The method of customizing cell through the Xib file is discussed here for the time being, if there is any need to add and correct it, please point out that we study together.

II) Customize the cell in a way that uses pure handwritten code

The case of customizing the cell using the pure code method is that each cell in the UITableView may display data that is not uniform, which is the need to customize the cell in a pure code way.

The method of implementing a custom cell with pure code is also very simple, and the difference between implementing a custom cell with Xib is that the xib uses a drag control to lay out the custom cell, and the pure code is to lay out the custom cell in a code way. The ideas are all the same.

1, the use of pure code to achieve a custom cell at least two models. ① Data Model ②frame model

i) The data model is used to convert a dictionary into a model, which is a simple dictionary-to-model.

II) The frame model is the one that sets the frame for a custom control in a custom cell, and also gets the row height.

2, you also need to create a new class, let this class to inherit UITableViewCell, this class describes the cell we want to customize.

In the class to override UITableViewCell-(Instancetype) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) Reuseidentifier method, specifically rewrite the Init method of writing I will not repeat here, I would like to say, we initialize the purpose of this method is to do something. The purpose of ① is to create all the controls that may appear in our custom cell, and to set some properties that can be set at once; ② Add the controls we created to Self.contentview. These two purposes are equivalent to dragging a control in the Xib file, but here we are using code to "drag" it.

"Attention" "Next we need to know the relationship between four objects first." Which four objects?

① Data Model ②frame Model ③ Custom cell class ④ controller

From left to right to say, ① data model the most bitter, he who can not control, there is only one role, that is to convert the dictionary into a model;

② next to the frame model, he is more than a data model, because in its internal reference a data model, "as long as the reference to a model will override the model this property setter method." So what do we need to do in the frame model, first we declare the properties of a data model, because we need the data in the data model, and then we declare the properties of the CGRect type as much as the number of controls, because we want to calculate the frame of each control and save it in the corresponding property , and the last to copy to the corresponding control's frame.

Finally, declare a property of type CGFloat, which is used to record the row height of the cell. "Note" That we are outside is not allowed to arbitrarily modify the control of the frame, so in the frame model, in addition to the properties of the data model, the other properties are declared as ReadOnly.

The last thing to do in frame is very simple, which is to calculate the frame of the corresponding control in the setter method of the data model, and record it.

③ custom cell class is even more awesome, he can directly or indirectly access the frame model and data model.

In a custom cell class, you also need to introduce a property of the frame model type, and you need to override his setter method.

So what do I need to do to rewrite the setter method?

i) set the frame value for the control defined in the cell;

II) Assign values to the controls defined in the cell;

Finally I have to reiterate again, be sure to rewrite-(instancetype) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) Reuseidentifier this method.

④ finally to the finale, that is the controller, he controlled the implementation of various logic, including lazy loading data, as a TableView data source, as a tableview agent and so on, all he is the control of the entire program.

So by observing the above analysis steps, all things are very simple, just step by step, all things can be changed very simple.

Well, too sleepy, tomorrow will have to class, that about the custom cell for a moment to discuss this, if there are mistakes and need to add to the hope that we can correct, we learn together.

Some summary of iOS

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.