[Go] iOS tableviewcell dynamic height adjustment

Source: Internet
Author: User

Original: http://blog.csdn.net/crayondeng/article/details/8899577

Recently encountered a cell height change problem, when looking for solutions, reference to this article, feel good

When writing Sina Weibo content, the cell is used to display it, so consider the cell height problem caused by different microblogging content. When you include text and pictures in the content displayed on Weibo, you calculate the height of the text section and the height of the picture. This blog post will record how to deal with the dynamic adjustment of cell height!

I. The traditional approach

Set the height of the TableView delegate in the method of setting-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: ( Nsindexpath *) Indexpath, of course, in this proxy method you need to calculate the height of the text and the height of the picture, and then return.

1, text (string) height of the processing;

Because the length of the text is indeterminate, it is necessary to display the height of the text according to the dynamic text length.

[CPP]View Plaincopy
    1. #define  FONT_SIZE 14.0F   
    2. #define  CELL_CONTENT_WIDTH 320.0f  
    3. #define  CELL_CONTENT_MARGIN 10.0f  
    4.   
    5. nsstring *string = @
    6. Cgsize size = cgsizemake (cell_content_width -  ( CELL_CONTENT_MARGIN * 2),  maxfloat);   
    7. cgsize textsize  = [string sizewithfont:[uifont systemfontofsize:font_size] constrainedtosize:size  linebreakmode:nslinebreakbywordwrapping];  


The above code is calculated text height. Where size is the container for loading text, where 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'll know the size of the string.


2, the picture height processing;
First you have to download to the picture, then cgsize imageviewsize = Cgsizemake (image.size.width,image.size.height), you can get the size of the picture, You can set the frame of your imageview, then you will know the height of the picture.

Ii. Non-traditional methods
In-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) The height of the frame of the cell is handled in the Indexpath proxy method, which is called in the method of setting the height of the TableView delegate, so the cell height can be set well. (Note the order in which the methods are executed: Heightforrowatindexpath This proxy method is executed first, then executes Cellforrowatindexpath)

[CPP]View Plaincopy
  1. -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
  2. {
  3. static NSString *cellidentifier = @"Cell";
  4. UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];
  5. if (cell = = nil) {
  6. //cell = [[UITableViewCell alloc] Initwithframe:cgrectzero reuseidentifier:cellidentifier];
  7. //This method has been Deprecated
  8. cell = [[UITableViewCell alloc] Initwithframe:cgrectzero];
  9. }
  10. CGRect cellframe = [cell frame];
  11. Cellframe.origin = cgpointmake (0, 0);
  12. Get the handling of the cell's height
  13. CellFrame.size.height = * * *;
  14. [Cell Setframe:cellframe];
  15. return cell;
  16. }


To explain a little bit, notice that the cell initialization is Cgrectzero, and then [cell frame] gets the cell frame, so that after the cell's height can be processed, Setframe re-set the cell frame.
The next step is to invoke it in the Heightforrowatindexpath method.

[CPP]View Plaincopy
      1. -(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath {
      2. UITableViewCell *cell = [self Tableview:tableview cellforrowatindexpath:indexpath];
      3. return cell.frame.size.height;
      4. }

[Go] iOS tableviewcell dynamic height adjustment

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.