IOS Controls-cell adaptive height in UITableView

Source: Internet
Author: User

When the content of a label in the UITableView is longer, it is necessary for the cell to adapt to the height to display the label in multiple lines;

First set the label line to 0;

The code is as follows:

//Pre-set a height for each cell to increase efficiency-(CGFloat) TableView: (UITableView *) TableView Estimatedheightforrowatindexpath: (Nsindexpath *) Indexpath {return  -;}//the actual height of each cell here (ie, adaptive height)-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath {//you can set the adaptive height individually for the line you need by using the IF Judgment statement.    if(Indexpath.row = = [Rowsarr count]-1) {                //the height required to get the label through the dataNSString *rowname =[Rowsarr ObjectAtIndex:indexPath.row]; NSString*key =[Rowsandkeydic Objectforkey:rowname]; CGFloat Descriptionh=[self textheight:[taskdetaildic objectforkey:key]]; returnDescriptionh + -;//This 70 is completely tuned to your situation .    }    return  -;}//Gets the height that the label actually needs-(CGFloat) TextHeight: (NSString *) LabelText {Uifont*tfont = [Uifont systemfontofsize:16.0]; Nsdictionary* dic =[Nsdictionary Dictionarywithobjectsandkeys:tfont,nsfontattributename,nil]; //iOS7 API to determine the height of labelText this string, the width here (self.view.frame.size.width-140he) as needed to change the OKCgsize Sizetext = [LabelText boundingrectwithsize:cgsizemake (Self.view.frame.size.width- $, +) Options:nsstringdrawinguseslinefragmentorigin Attributes:dic context:nil].size; returnsizetext.height;}

IOS Controls-cell adaptive height in UITableView

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.