You may encounter such a problem or need such a feature.
The height of each cell varies with the content of the cell. Especially when the content is not just text
In fact, it is very easy to implement this function.
First of all, the so-called Dynamic Allocation of cell height only looks like this in effect, but it is still common with us to set
When the cell height is the same, the height is allocated to each row first.
Here is a demo.
The most important thing to implement this function is the following two methods:
1. // set the Row Height
-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath
{
NSString * text = .......;
// The text content here is the content you want to display on the cell
CGSize constraint = CGSizeMake (300,200 0.f );
// Set a constraint first to calculate the size of the view occupied by text content when the width is specified.
// Set the height as high as possible, because you are not sure about the specific content of text.
CGSize size = [text sizeWithFont: [UIFont systemFontOfSize: 14] constrainedToSize: constraint lineBreakMode: NSLineBreakByClipping];
// Here, an NSString method is used to calculate the size of text,
// Thanks to NSString for encapsulating this method for us to implement this function
CGFloat height = MAX (size. height, 40366f );
// This is to set a minimum value for the cell height.
Return height;
}
2. // configure each unit
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
{
UILabel * textLabel = nil;
// First create a label for displaying text. Of course, you can also use other labels,
Static NSString * cellIndentifier = @ "indentifier ";
// Create an exclusive label for the Cell
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: cellIndentifier];
// First search for the buffer zone to see if there is any cell with the exclusive tag
If (cell = nil ){
// Create
Cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: cellIndentifier];
// Initialize the tag
TextLabel = [[UILabel alloc] initWithFrame: CGRectZero];
TextLabel. font = [UIFont systemFontOfSize: 14];
TextLabel. tag = 1;
// Set the tag value for future calls.
// Add the label to the cell
[[Cell contentView] addSubview: textLabel];
}
NSString * text = ......;
// Same as above
CGSize constraint = CGSizeMake (300,200 0.f );
CGSize size = [text sizeWithFont: [UIFont systemFontOfSize: 14] constrainedToSize: constraint lineBreakMode: NSLineBreakByClipping];
If (! TextLabel ){
TextLabel = (UILabel *) [cell viewWithTag: 1];
}
// If the label is empty, call the label value.
TextLabel. text = text;
TextLabel. frame = CGRectMake (10, 10,300, size. height );
// The area in which the label is displayed by setting the size of the text content
Return cell;
}
The core function is the above two methods. Is it very simple? Of course, this is only plain text. What if there are still images mixed?
In fact, the principle is the same. UIImage also has a property size. You should know what to do if you say this?
Now, I will attach a demo about the image. If you still don't understand it, you can discuss it with me.
This is the demo. You can check the effect.
Link to this demo: http://download.csdn.net/detail/u012884714/6795293
I encountered this problem and checked the information to implement this function. Here I will share with you-LC