Simple custom UITableViewCell and mvcuitableviewcell of MVC Architecture

Source: Internet
Author: User

Simple custom UITableViewCell and mvcuitableviewcell of MVC Architecture
In the iOS development process, architecture is an important part. The current mainstream is divided into two types: MVC and MVVM, which are not divided too much here, if you are interested, you can take a look at an article by Dr. Tang Qiao, "Misunderstood MVC and materialized MVVM". UITableViewCell should be one of the most used controls in iOS development, and the MVC mode is also the mainstream. Therefore, the combination of the two will greatly improve the quality of our code. According to this logic, Model-View-Controller can clearly divide the code, and the corresponding classes only do the work, which not only greatly improves code coupling, it facilitates later maintenance. First, we create a new class named MyCell, which inherits from the UITableViewCell class. We can customize the structure we want to customize in this class. In the future, the Controller only needs to create an object for this class. Recalling the previous reuse mechanism of UITableViewCell, we can easily think of the initialization method. We will rewrite the initialization method with the Code as follows:-(id) initWithStyle :( UITableViewCellStyle) style reuseIdentifier :( NSString *) reuseIdentifier {

Self = [superinitWithStyle: style reuseIdentifier: reuseIdentifier];

If (self ){

// Initialization code

[Self createUI];

}

Returnself;

}

Here we will customize the UI page, write a separate method, and then perform our own Customization

 

-(Void) createUI {

_ IconView = [[UIImageView alloc] initWithFrame: CGRectMake (5, 5, 70, 70)];

[Self. contentView addSubview: _ iconView];

_ NameLabel = [[UILabel alloc] initWithFrame: CGRectMake (80, 5,230, 25)];

_ NameLabel. font = [UIFont systemFontOfSize: 16];

_ NameLabel. textColor = [UIColor blackColor];

[Self. contentView addSubview: _ nameLabel];

_ PriceLabel = [[UILabel alloc] initWithFrame: CGRectMake (80, 30,230, 25)];

_ PriceLabel. font = [UIFont systemFontOfSize: 14];

_ PriceLabel. textColor = [UIColor redColor];

[Self. contentView addSubview: _ priceLabel];

_ DetailLabel = [[UILabel alloc] initWithFrame: CGRectMake (80, 55,230, 25)];

_ DetailLabel. font = [UIFont systemFontOfSize: 12];

_ DetailLabel. textColor = [UIColor grayColor];

[Self. contentView addSubview: _ detailLabel];

}

We can see that on the current cell, we have customized a UIImageView to load the image, and three UILable to display the corresponding information. Generally, the Controller uses self. view addSubview to load the control. Similarly, UITableViewCell is also a View, but it cannot use view, but instead uses contentView to load it. So we can implement the design we need in our cell.

In daily development work, data is rarely written to death. Generally, data is received from the background through requests, and then the data is loaded to the corresponding control as needed, so for us, the control is variable, and the data on the control is variable. In this way, we extract the required data separately to build a model. Analyze the requirements and code. What we need now is a UIImageView image and three UILable text information. Therefore, we extract these attributes separately as our cell model.

Create a new MyModel class that inherits from NSObject and contains the attributes of our cell. The Code is as follows:

@ Property (retain, nonatomic) UIImage * icon;

@ Property (copy, nonatomic) NSString * nameStr;

@ Property (copy, nonatomic) NSString * priceStr;

@ Property (copy, nonatomic) NSString * detailStr;

Note: Because this class needs to be referenced by other classes and accept data, it should be written under. h file and interface.

Here, the idea of MVC is coming out, but there is another question: how to associate cell with your own model. We need to expose a method to the cell to receive the model. For the same reason, we declare this method in the cell. h file:

-(Void) mwmwithmymodel :( MyModel *) bm;

Then implement this method in the cell. m file. The parameter type is the custom model class.

 

-(Void) mwmwithmymodel :( MyModel *) bm {

_ IconView. image = bm. icon;

_ NameLabel. text = bm. nameStr;

_ PriceLabel. text = bm. priceStr;

_ DetailLabel. text = bm. detailStr;

}

So far, our custom part is basically over, and the next step is to use our custom Cell in the Controller.

 

-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {

StaticNSString * idt = @ "idt ";

MyCell * cell = [tableView dequeueReusableCellWithIdentifier: idt];

If (! Cell ){

Cell = [[MyCell alloc] initWithStyle: UITableViewCellStyleDefaultreuseIdentifier: idt];

}

MyModel * mm = [_ dataArrobjectAtIndex: indexPath. row-1];

// Pass the data model to the cell to set the image and text for the cell

[Cell mwmwithmymodel: mm];

Return cell;

}

In this way, we can create a custom cell that uses the MVC ideological architecture. How can we connect data? That's another note. Haha.

 



 

 

 

 


 

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.