Dynamically adjust the height of IOS tableviewcell

Source: Internet
Author: User

When you use cell to display Weibo content on Sina Weibo, consider the cell height problems caused by different Weibo content. If the content displayed on Weibo contains text and images, the height of the text part and the image part must be calculated. This blog post will record how to deal with the dynamic adjustment of cell height!

I. Traditional Methods

Set-(cgfloat) tableview :( uitableview *) tableview heightforrowatindexpath :( nsindexpath *) indexpath, of course, in this proxy method, you need to calculate the text height and image height, and then return.

1. Processing of text (string) height;

Because the length of the text is uncertain, the height of the text must be displayed on the computer based on the dynamic length.

# Define font_size 14.0f # define cell_content_width 320366f # define cell_content_margin 10.0 fnsstring * string = @ "Text to be displayed"; cgsize size = cgsizemake (cell_content_width-(Limit * 2 ), maxfloat); cgsize textsize = [String sizewithfont: [uifont systemfontofsize: font_size] constrainedtosize: Size linebreakmode: nslinebreakbywordwrapping];

The above code calculates the text height. The size is the container for loading text, and the height is set to maxfloat.
-(Cgsize) sizewithfont :( uifont *) font constrainedtosize :( cgsize) size linebreakmode :( uilinebreakmode) linebreakmode
Returns the size of the string if it were rendered with the specified constraints. you can know the size of the string.

2. Image Height processing;
First you need to download the image, then cgsize imageviewsize = cgsizemake (image. size. width, image. size. height); you can get the size of the image, you can set the frame of your imageview, then you know the height of the image.

Ii. Non-Traditional Methods
In-(uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) the height of the frame in which the cell is processed in the indexpath proxy method, call this method in the delegate setting height method of tableview to obtain the configured cell height. (Note the execution sequence of the two methods: heightforrowatindexpath: the proxy method is executed first, and then cellforrowatindexpath is executed)

-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {static nsstring * handle = @ "cell"; uitableviewcell * cell = [tableview progress: cellidentifier]; if (cell = nil) {// cell = [[uitableviewcell alloc] initwithframe: cgrectzero reuseidentifier: callback]; // This method has been deprecated cell = [[uitableviewcell alloc] initwithframe: cgrectzero];} cgrect cellframe = [Cell frame]; cellframe. origin = cgpointmake (0, 0); // obtain the height of the cell to process cellframe. size. height = ***; [Cell setframe: cellframe]; return cell ;}

To explain it a little, we noticed that the cell was initialized with cgrectzero, and then [Cell frame] was used to obtain the cell's frame, so that after the cell height can be processed, setframe: reset the frame of the cell.
Next, call the heightforrowatindexpath method.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];    return cell.frame.size.height;}

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.