Xib dynamically changes the cell Row Height and xib dynamically changes the cell Row Height.
The layout is as follows:
Three Controls in total, one Avatar, one background, and one message
Now, when multiple lines of text are input, you want the cell to dynamically adjust the Row Height.
First, the height of the Avatar must not be changed. Therefore, the width of the Avatar is determined by the width of contentview. The height of the Avatar is (others can also) depending on its width, in this way, the height of the contnetview does not affect the height of the Avatar.
Setting uilabel to display multiple rows is somewhat different.
Reference
Automatic line feed of UILabel under XIB (dynamic line feed and height adjustment are supported)
Use Autolayout to implement the Cell dynamic Layout and height dynamic Change of UITableView (this is translation) Using Auto Layout in UITableView for dynamic cell layouts & variable row heights (this is the original article)
According to the above suggestions, cell creation will be called multiple times in advance
-(CGFloat)tableView:(nonnull UITableView *)tableViewheightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
It is very important to set the height before creating a cell.
This is generally the case if it is pure code.
First, the data source is obtained before the cell is created. Then, the data source finds the data according to the cell index, and then processes the obtained data to be returned in heightForRow.
-(CGFloat)tableView:(nonnull UITableView *)tableViewheightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{ NSString* string=dataSource.nsArrayModels[indexPath.row]; CGSize size=[string boundingRectWithSize:CGSizeMake(tableView.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.f],NSBackgroundColorAttributeName:[UIColor yellowColor]} context:nil].size; return size.height;}
If the plain text information is processed properly, it is troublesome to change the data style and the attribute Configuration
@{NSFontAttributeName:[UIFont systemFontOfSize:12.f],NSBackgroundColorAttributeName:[UIColor yellowColor]
It is based on the content in the xib, which is invalid here (why write? If you forget to write the code, you will be in trouble)
Xib adaptive
First, how to set the height
// Enable the cell height self. tableview0.estimatedRowHeight = 20.f; self. tableview0.rowHeight = UITableViewAutomaticDimension dynamically based on the content;
Yes. You can use the row-Height Estimation Method provided by the system to obtain a temporary cell height. When the cell actually changes the height based on constraints and is created, the height is automatically replaced.
The key to changing rows is
Make sure that the content compression resistance (compression resistance) and hugging constraints in the vertical direction of each sub-view are not overwritten by the constraint conditions with higher priority you have added, enable the inherent content size (intrinsic content size) of these subviews to drive the height of the contentView.
Content-driven, with the highest priority (at least on the matter of changing the height). Note that content-driven means that all controls that directly affect the height of contentview will be affected here, if the pixel height of the background image is large enough or equal to the height of the cell, the portrait will be affected, in order to keep the text completely displayed on the background, the background height is driven by message.
It's really an accident that the built-in emoticons can be directly displayed.
The width does not seem to have been done .........