"iOS Knowledge learning" _ios dynamically changing TableView cell height

Source: Internet
Author: User

When doing tableview, we sometimes need to adjust according to the cell's high dynamic, recently saw a code on the Internet good, with everyone share.

In

-(uitableviewcell *) TableView: (uitableview *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{

Gets the height of the cell in the class:
    Cgsize boundsize = Cgsizemake (216, Cgfloat_max);    Cell.textLabel.text = @ "12345678900123456789";    cell.userinteractionenabled = NO;    Cell.textLabel.numberOfLines = 0;    Cgsize requiredsize = [Cell.textLabel.text sizewithfont:[uifont systemfontofsize:13] Constrainedtosize:boundsize Linebreakmode:uilinebreakmodewordwrap];    CGRect rect = cell.frame;    Rect.size.height = requiredsize.height+5;    Cell.frame = rect;
The height of the cell is obtained, and then the

-(cgfloat) TableView: (uitableview *) TableView Heightforrowatindexpath: (nsindexpath *) Indexpath {

Class to change the cell's height:
    UITableViewCell *cell = [self Tableview:tableview cellforrowatindexpath:indexpath];        NSLog (@ "Cell height%f", cell.frame.size.height);    return cell.frame.size.height;

Since then, the cell's height has changed automatically based on the contents of the label in the cell.

The main starting point is that I have a label, and then I'm going to show this label, and I'll get a height based on the size of the font and the number of rows, so the cell's height is there.



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.