Masonry Tableviewcell Layout

Source: Internet
Author: User

Reprinted from: http://www.henishuo.com/masonry-tableviewcell-layout/preface

When iOS it comes to automatic layout, there are a lot of workarounds. Some people use xib/storyboard automatic layout, and others use it frame to fit. For the former, I do not like, nor support. For the latter, it is troublesome, everywhere calculate height, width, etc., tens of millions of redundant code, the maintenance and development of the efficiency are very low.

The author introduces the third-party library of pure Code automatic layout here: Masonry . The use of this library is very high, there are a large number of developers in the world in use, the star number is also quite high.

This section details Masonry the basic usage of updating constraints in animations, first look at:

Here we have the initial button is a very small button, click on the zoom, the largest zoom to the full screen.

Core code

Look at the code:

#import "TableViewController.h"#import "TestCell.h" @interfaceTableviewcontroller () <uitableviewdatasource, uitableviewdelegate>@property (nonatomic, strong) UITableView*TableView, @property (nonatomic, strong) Nsmutablearray*DataSource;@end @implementationTableviewcontroller- (void) viewdidload {[Super viewdidload]; Self.tableview=[[UITableView alloc] init]; Self.tableView.separatorStyle=Uitableviewcellseparatorstylenone; Self.tableview.Delegate=Self ; Self.tableView.dataSource=Self ;  [Self.view AddSubview:self.tableView]; [Self.tableview mas_makeconstraints:^ (Masconstraintmaker *Make )  {Make.edges.mas_equalTo (Self.view);    }];  for(Nsuinteger i =0; I <Ten; ++i) {Testmodel*model =[[Testmodel alloc] init]; Model.title=@"test the title, it may be a long, long, anyway, just write it first! "; Model.desc=@"The description content is usually very long, the description content is usually very long, the description content is usually very long, the description content is usually very long, the description content is usually very long, the description content is usually very long, the description content is usually very long, the descriptive contents are often very very long , The description is usually very long, the description content is usually very long, the description content is usually very long, the description content is usually very long,";  [Self.datasource Addobject:model]; } [Self.tableview reloaddata];} -(Nsmutablearray *) DataSource {if(_datasource = =Nil) {_datasource=[[Nsmutablearray alloc] init]; }    return_datasource;} #pragmamark-uitableviewdatasource-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {returnSelf.dataSource.count;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {StaticNSString *cellidentifier =@"Cellidentifier"; Testcell*cell =[TableView Dequeuereusablecellwithidentifier:cellidentifier]; if(!cell) {Cell=[[Testcell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } Cell.indexpath=Indexpath; Cell.block= ^ (Nsindexpath *path)  {[TableView Reloadrowsatindexpaths:@[path] withrowanimation:uitableviewrowanimationfade];  }; Testmodel*model =[Self.datasource ObjectAtIndex:indexPath.row];    [Cell Configcellwithmodel:model]; returnCell;} -(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath {Testmodel*model =[Self.datasource ObjectAtIndex:indexPath.row]; return[Testcell Heightwithmodel:model];} @end

Explain

Let's take a look at the code that calculates the line height, does it look like a proxy method for configuring data?

-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath {  * Model = [Self.datasource objectAtIndex:indexPath.row];     return

Let's look at TestCell the statement that provides a class method for calculating row heights:

void (^testblock) (Nsindexpath *@interface* * *-(void) Configcellwithmodel: (Testmodel *+ (cgfloat) Heightwithmodel: (Testmodel *@end

Let's take a look at the implementation of the calculation line height:

+ (CGFloat) Heightwithmodel: (Testmodel *) model {  *cell = [[Testcell alloc] Initwithstyle: Uitableviewcellstyledefault reuseidentifier:@ ""];  [Cell Configcellwithmodel:model];    [Cell layoutifneeded];     =  cell.descLabel.frame;   return  - ;}

We just created one cell and then configured the data and then called the layoutIfNeeded Update constraint in order to get to it frame . When we get to it, we can figure out the final cell true height.

On the calculation cell of the row height, I open up an extension class, of course, inside the company is also used by everyone, can reduce a lot of code. If you need to use it, you can download it here: Https://github.com/CoderJackyHuang/HYBMasonryAutoCellHeight or use the cocoapods installation directly.

Source

Everyone can go to the author's github download source code: Masonrydemo

Note : The content in this section corresponds to TableViewController the content in

Masonry Tableviewcell Layout (RPM)

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.