问题
I am working with Tableview
in Objective C
.
I have 4 labels in a TableView cell. I want to expand the Comment Body label and Answer Body label according to the content size. How it is possible using AutoLayout. Please help me
How to set dynamic height of label as per content?
The ScreenShot of the tableview
is given below.
回答1:
Your constraint should be like,
Comment
-> top, leading(left side), fixed width, fixed height
Answer
-> top, leading, fixed width, fixed height
Comment Body
-> top, leading, trailing (right side), fixed height (greater than or equal to some constant value), bottom
Answer Body
-> top, leading, trailing, bottom
Make sure that number of lines for comment body
and answer body
must be 0
.
and you have to set estimatedrowheight and row height for your tableview like,
self.yourTableView.estimatedRowHeight = 50;
self.yourTableView.rowHeight = UITableViewAutomaticDimension;
OR
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50;
}
- (CGFloat)tableView:(UITableView *)_tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
回答2:
Constrain
Comment Body :- Top, Trailing, Bottom
Answer Body :- Top, Trailing, Bottom
comment :- Center y to Comment Body, leading, Horizontal to Comment Body , Fix width
Answer :- Center y to Answer Body, leading, Horizontal to Answer Body, Fix width
numberOfLine
0 for both dynamic label
Code
Add Following code to ViewDidLoad
or ViewWillAppear
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 80;
Note: Remove heightForRow
来源:https://stackoverflow.com/questions/46439411/self-growing-label-in-tableview-cell