Preface : This small example project is completely based on MJ Video taught optimization program step by step, one idea, from scratch to knock out the code , And every step has an idea , It's all logical, so . Knock code, pay attention to thinking , I personally don't recommend remembering too much , Anyway, I have a bad memory .

Tips: : At the end of the article Project resources Baidu cloud download link .

Small case projects -- Add merchandise and trade name

good , Now it's done plist File data -- The switch to --> Collection objects store this data , Next, according to MVC Principles , Of course we need a Model( Data model ),

So you need to create a new data model class , At the same time, we need the class prefix , Class prefix can be set once and for all :

Set the class prefix :

New data model :

Come here MVC Medium Model Be on it , And then we have to prepare View 了 :

In order to share the control with multiple controllers

  • Inherited from the control of the system , Write a control of your own
  • Purpose : Encapsulate the details inside the control , Don't let the outside world care
  • step :
    • Create a new inheritance UIView Class
    • stay init Method
    • stay layoutSubViews Method frame
      • Be sure to call [super layoutSubView]
    • Provide a model property ( I personally found that : In fact, you can directly pass in local parameters through the initialization method , And then the local part uses this model property ), Rewriting model properties set Method
      • stay set Method , Assign values to the corresponding child controls
 Because inherited from UIView, So it has UIView Properties of .
such as frame attribute , such as addSubView Method , And you can override the layoutSubViews Method

layoutSubviews This method is specifically used to lay out child controls , Set the frame

// This method has to be implemented
[super layoutSubviews];

Think about custom controls in this MVC How to customize and use the integrated project :

1、 First of all, because of the combination pattern , So you need to create two controls that need to be combined internally :
UIImageView and UILabel These two child controls .
2、 Where to create these two combined controls ?
Create the most appropriate... In the initialization method . Because we need to create this custom control , You create two child controls .
3、 in addition , Creating child controls requires model data model, Because a UIImageView Corresponding to one icon, One UILabel Corresponding to a trade name ,
And one icon And a UILabel Is stored in a model In the model object , therefore , You also need to create a reference to the model object .


please On : Next, let's talk about why we should be in layoutSubView in , Implementation of child control frame Set up , Not just in the initialization method WithFrame: (CGRect)frame Pass in the required parent control frame, And then through the frame Set the frame Well ? Or by declaring a property Of Frame attribute , And rewrite this frame Of set Method :

1 -(void)setFrame:(CGRect)frame
2 {
3 _frame = frame;
4 }

Explain answer : Because the " Discuss " The problem is that , No matter what is passed into the parent control through the initialization method frame, Or access symbols through dots or setFrame Method to pass in the frame Will be called layoutSubView Method , also , In the class of the parent control , The initialization method also calls its own through the point access symbol frame, Just click to visit frame Will be called layoutSubView Method , So it's better to go straight to layoutSubView Set the layout position of the child control .

in addition : as long as Of a control frame A change , Will call layoutSubView Method , This can be done by clicking the event button , Button to listen for a change component frame Attribute method , This side Method implementation, click once to change the control frame, And then in the class of this control layoutSubView Method write print frame Methods .

So the idea that the following code doesn't change is , In the initialization method of the parent control class, only through alloc-init To initialize the child controls , And then in layoutSubView Method frame.

The idea of Jiugongge algorithm :

 Logical process description : Every click , Add a picture once , And adding a line more than three times will wrap , And it occupies one third of the row distance of the parent control .
We need to solve three problems :
1、 Every click , Add a picture once
2、 Add more than three line breaks per line
3、 The location is one third of the row distance of the parent control

problem 1 Solutions : Every click , The change is to add child controls 1, So you can get the dynamic data value through the number of child controls of the parent control n :

int n = (int)self.shopsBoxView.subView.count

problem 2 Solutions : As long as it's related to line feed, you can consider finding remainder or modulus :

// Row number :row Column :column
int row = n / 3;
int column = n % 3;
 And then there are the coordinates of this grid in the nine palace grid 
Combined with the order of the number of corresponding child controls
  • Let's briefly explain the logical relationship , Before we first add child controls , Number of child controls count = 0, The coordinates corresponding to the child controls to be added are (0,0), therefore :count = 0 -> (0,0);count = 1 -> (0,1);count = 2 -> (0,2)... And so on

summary : Use the current number of child controls , Through the algorithm of finding the row and column , You can calculate the coordinates of the corresponding nine palace grid

problem 3 The solution :

The coordinates obtained in question 2 can be used to calculate the coordinates of the sub controls to be added .

On the code algorithm ideas ( In addition, after the button is written directly, when the number reaches the target value, it will automatically become disEnable state ):

The above errors are corrected : The number of lines of code for the nine palace algorithm should be 75~92 That's ok .

Then back HYUIView Class , Make the final optimization .

  Come here , Pure code, the best project code is completed , In fact, if you use xib Word of the file , It will make the whole project development easier and more perfect , The next blog post will be broken down on the basis of this project xib And if you optimize this project , Finally, it makes you feel like development IOS Really good .
Pure code source file download link : password : ymhc
Pure code resource file download link : password : 1squ

IOS And UI-- Small case projects -- Add merchandise and trade name ( The end of pure code ) More articles about

  1. stay iOS In development , Add a new... To the project .framework

    First of all, I need to understand iOS Static library and dynamic library .framework The concept of The difference between static library and dynamic library First, let's see what a library is , library (Library) To put it bluntly, it's a piece of compiled binary code , With the header file, it can be used by others . When can I ...

  2. IOS And UI-- Small case projects -- Add merchandise and trade name

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  3. IOS And UI-- Small case projects -- Add merchandise and trade name ( Use xib The end of the file ) + xib Summary of relevant knowledge points

    Add merchandise and trade name items ( Use xib The end of the file ) Tips: : Blog at the end of the project source code in Baidu cloud backup download link . xib Summary of relevant knowledge points 01- Basic use First use xib When , If you want to use custom view Code for , Need ...

  4. 【iOS Development 】 Automatic adaptation of multi screen ruler AutoLayout ( Pure code mode )

    About AutoLayout, Earliest from iOS6 Start using .   The main function is to use constraints , Relative layout of views , In order to adapt to the transformation of different screen ruler .   A lot of materials are introduced on the Internet xib and storyboard, How to use AutoLa ...

  5. ios Gesture review value for picture - Transition animation ( Pure code )

    The goal is : Realize the switching of pictures through gestures   By scanning left and right To achieve ( Pure code ) Add three properties 1uiImageView Used to show pictures view 2 index An index used to represent a picture 3 ISLeft Judge whether or not to ...

  6. IOS Small examples of development -- Create a simple application for video recording and playback

    Preface : Let's take a look at the introduction to foreign countries IOS article :<Create a Simple App for Video Recording and Playback>, It mainly involves the basic realization of video recording and playback function ...

  7. IOS Small examples of development -- Use UIImagePickerController Create a simple camera application

    Preface : I read this blog article abroad IOS Programming Tutorial Summary of the learning process of an introductory article in , Difficulty is not great , Because it's the introduction . It's mainly about getting started UIImagePickerController This controller , that ...

  8. iOS Development : by xcode Project add git Storage

    Now? apple Download from the official website Command Line Tools Corresponding mac Version and xcode edition , Record the address : find mac Terminal ,c ...

  9. 【UI】 Add something similar to Taobao filter list check for the project ui-choose

    jQuery Download address : GitHub Address : ...

Random recommendation

  1. XFire Best practices

    Preface :XFire Is the new generation WebService frame , At the same time, it supports Spring Integrate , Help us conveniently and quickly in Spring Framework WebService application . This section focuses on the introduction XFire+Spring Integrated 2 Common use ...

  2. gvim How to display html Property code prompt ? vim How to display javascript Properties and method tips ?

    gvim How to display html Property code Can be in vim in Show html, css, js Etc / Method Tips : One is : stay ~/.vim/after/syntax/ Directory install css-color.vim ...

  3. Comet Technical details : be based on HTTP Long connection Web End real-time communication technology

    Preface Generally speaking ,Web Due to the limitation of browser design, the end-to-end instant messaging technology is limited , It's not always easy to implement , Mainstream Web The end instant messaging solutions are as follows 4 Kind of : Tradition Ajax Short polling .Comet technology .WebSocket technology .SSE(Ser ...

  4. __ATTRIBUTE__ How much do you know? ?【 turn 】

    from : GNU C  One of the main features of this project is __attribute__  Mechanism .__attribute__  You can set the function attribute ...

  5. Python Support vector machines in SVM Use ( There are examples )

    In addition to the Matlab Use in PRTools In the toolbox svm Algorithm ,Python You can also use support vector machine to do classification . because Python Medium sklearn It is also integrated. SVM Algorithm . One . A brief introduction sklearn Scik ...

  6. css3 Graph drawing

    The following examples mainly use css3 in border.bordr-radius.transform. And so on , Let's first understand the basic principles of them . border: In a nutshell border Grammar mainly includes (bo ...

  7. json&amp;pickle modular

    serialize : We put the object ( Variable ) The process of changing from memory to storable or transportable is called serialization Deserialization : Reread variable contents from serialized objects into memory , This process is called deserialization Why serialize ? 1. Persistent save state The execution of a software is at ...

  8. 17 autumn Software Engineering The fifth assignment of the team Alpha

    subject : Team work --Alpha sprint 17 autumn Software Engineering The fifth assignment of the team Alpha 12 Time Scrum for the first time Scrum The second time Scrum third time Scrum The fourth time Scrum The fifth time Scrum The sixth time Scr ...

  9. Safety series CSRF On

    Preface Security for Internet Professionals , It has always been a relatively weak part in contact or actual use , Of course, for those special technical personnel engaged in safety, the exception is . Next, let's talk about some common types of security vulnerabilities and some preventive measures in the field of Internet , Needle, of course ...

  10. SQL mysql Optimize

    The slow query How to find problems through slow log search SQL? The number of queries is large and each query takes a long time SQL pt-query-digest Analyze the first few queries IO Big SQL pt-query-diges In the analysis Rows exa ...