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
- #define  FONT_SIZE 14.0F
- #define CELL_CONTENT_WIDTH 320.0f
- #define CELL_CONTENT_MARGIN 10.0f
-
- nsstring *string = @
- Cgsize size = cgsizemake (cell_content_width - ( CELL_CONTENT_MARGIN * 2), maxfloat);
- 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
- -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
- {
- static NSString *cellidentifier = @"Cell";
- UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];
- if (cell = = nil) {
- //cell = [[UITableViewCell alloc] Initwithframe:cgrectzero reuseidentifier:cellidentifier];
- //This method has been Deprecated
- cell = [[UITableViewCell alloc] Initwithframe:cgrectzero];
- }
- CGRect cellframe = [cell frame];
- Cellframe.origin = cgpointmake (0, 0);
- Get the handling of the cell's height
- CellFrame.size.height = * * *;
- [Cell Setframe:cellframe];
- return cell;
- }
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
- -(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath {
- UITableViewCell *cell = [self Tableview:tableview cellforrowatindexpath:indexpath];
- return cell.frame.size.height;
- }
[Go] iOS tableviewcell dynamic height adjustment