UITableView dynamically calculates cell height using AutoLayout

Source: Internet
Author: User

UITableView is almost every app needs to use the control, and cell height adaptive is what we need to master, of course, the cell above the control is also a variety of manifestations, the small part of today is one of the more common: Auto Layout with UILabel in UITableViewCell.
Words not much to say, on the tutorial.
First we create a Sigle View application project, then drag the previous UITableView to storyboard, set up the proxy and set the constraints. Constraints here do not explain, you can go to AutoLayout to understand, and then back to our project, we create a inheriting UITableViewCell class, here I named Diytableviewcell, Then drag a uitableviewcell to the UITableView on the storyboard, then associate the cell with the Diytableviewcell class we created, what? Not associated? It's okay, look here!

And then give the cell a duplicate ID, and here I'm named cell

Next we place a label and a uiimageview on the cell and set the constraint, and note here that the Uilabel's NumberOfLines property should be set to 0.
Now we should add material to Viewcontroller, back to our Viewcontroller, implement UITableView proxy method,

`#pragma mark-uitableviewdatasource-(nsinteger) TableView: (uitableview *) TableView Numberofrowsinsection: (nsinteger) section{return self.tbdata.count;} -(uitableviewcell *) TableView: (uitableview *) TableView Cellforrowatindexpath: (nsindexpath *) indexpath{Diytableviewcell *cell = [TableView Dequeuereusablecellwithidentifier:iden]; Cell.titlelabel.text = [self< Span class= "hljs-variable" >.tbdata objectatindex:indexpath.row]; return Cell;} `
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

Here is an issue that requires special attention and is also a matter of efficiency. UITableView is a one-time calculation of the height of all cells, if there are 100 cells, then - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath it will be triggered 100 times before the content is displayed. After iOS7, however, a new method is provided to avoid these 100 calls, which is - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath . This method is required to return an estimate of a cell, and only the displayed cell will trigger the protocol of the calculated height. Since Systemlayoutsizefittingsize requires an instance of the cell to calculate, a cell's real column is stored here with a member variable, so that you do not need to dynamically generate a cell instance each time the cell height is computed. This is convenient and efficient and less memory, can be described as three. The optimization of UITableViewCell can be seen here to optimize the UITableViewCell height calculation

Next we declare an instance variable like this:

@property (nonatomic, strong) DIYTableViewCell *cell;
    • 1

Then instantiate it:

self.cell = [self.tableView dequeueReusableCellWithIdentifier:iden];
    • 1

Here is the code that calculates the cell height:

- (CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (nsindexpath *) indexpath{Diytableviewcell *cell = (Diytableviewcell *) self.cell; Cell.titleLabel.text = [self.tbdata ObjectAtIndex: Indexpath.row]; cgsize size = [Cell.contentview Systemlayoutsizefittingsize:uilayoutfittingcompressedsize]; nslog (@ "invocation Count%ld", Indexpath1); return MAX (44, 1 + size.height);              
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

Maybe you wonder why you need to add 1? I tell you that if you do not add 1, the result is wrong, the cell Uilabel will be displayed incorrectly. The reason is because this line of code cgsize size = [Cell.contentview systemlayoutsizefittingsize:uilayoutfittingcompressedsize]; Cell.contentview This method, the returned value will be contentview height, UITableViewCell height is 1 higher than its contentview, which is the height of its divider line.
Here our cell adaptive heights are complete, and let's look at the results of our operation:

Domo Source has been uploaded, if necessary can download Autolayoutcell

UITableView dynamically calculates cell height using AutoLayout

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.