MVVM Framework Idea

Source: Internet
Author: User

What is 1.MVVM?
M: Model
V: View
VMS: View model

Simple to understand: MVC is a cell for a model development
MVVM is a cell that is developed for a viewmodel, and ViewModel contains model
MVVM Pros: More detailed extraction methods, clearer division of business logic, and more lightweight controller
Cons: not conducive to maintenance, readability is not very good

Use of 2.MVVM frames (for example, to calculate unequal cells)
The above analysis guide, calculate the cell height to be in the network request memory

2.1 Customizing a TopView model
Define member properties The data that the receiving server returns (to be displayed on the view)

2.2 Customizing a ViewModel model
Define a TopView model property to save the model data
Define some properties to save the computed data (e.g., cell height and frame)

2.3 Data request succeeds, first convert the dictionary array we need into the model array

2.4 Then convert the model array into an array of view models and save them (define an array member variable)

1 // model turn View model 2  for inch themes) {3 Xmgthemeviewmodel *VM = [[Xmgthemeviewmodel alloc] init]; 4 // Calculating cell child controls frame and Cellh 5 Vm.item = item; 6 [Self.themeviewmodels ADDOBJECT:VM];

Note: When assigning a value to a view model (Vm.item = Item;), the View Model member property is triggered by the set method of item
We are in the set method of the item to calculate the cell frame and height,
That is, after the model has all changed to the view model, the cell's frame and height are all determined
You only need to save the frame and height in the View Model definition member property, and then take it directly.

3. How do I display the data?

3.1 Define a view model to receive data in a custom cell

3.2 Assigning a view model to a cell within the Cellforrow method (by removing the corresponding view model from the array)

1 cell.vm = _themeviewmodels[indexpath.row];

3.3 When assigning a value to the cell's view model, the set method of the VM is triggered and the VM's set method is called
Assign a value to the TopView model in the Set method and remove the cell frame and height

1 -(void) SETVM: (Xmgthemeviewmodel *) VM2{3 _VM = vm; 4 // TopView: Transfer model 5 _topview.item = Vm.item; 6 // Set Topviewframe 7 _topview.frame = vm.topviewframe; 8 }

3.4 How to assign a value to a model (i.e. show data)
The set method that triggers the model in the SETVM method _topview.item = Vm.item;
In other words, this code passes the server data saved by the view model to the model properties defined in the TopView

3.5 in the Set method of the TopView item, we complete the process of assigning a value to the cell's child control

1 -(void) SetItem: (Xmgthemeitem *) Item2{3 _item = item; 4 5 [_iconview Sd_setimagewithurl:[nsurl URLWithString:item.profile_image]; 6 _nameview.text = item.name; 7 _timeview.text = item.create_time; 8 _textview.text = item.text; 9 }

These child controls of the TopView are obtained by connecting them from the Xib

3.6 Where to assign the cell height?
Inside the Heightforrow method

1 -(cgfloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath;0


1 -(cgfloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath2 {3 Xtthemeviewmodel *VM = _themeviewmodels[indexpath.row]; 4 return Vm.cellh; 5 }

MVVM Framework Idea

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.