问题
Hello I have a UITableView with x# of cells. the last cell I have two UILabels. When the second UILabel text is set I am trying to get the cell and the UILabel to resize to show the text.
Here is what I have:
The UILabel - LabelBio (orange) has:
Lines: 0 Baseline: Align Baselines Line Break: Word Wrap Autoshrink: Fixed Font Size
The constraints for ContentView, LabelSellerInfo and LabelBio are set as follows:
LabelSellerInfo
LabelBio
ContentView
With those settings here is what a get:
I have tried many variations but cannot seem to get the Bio label to grow and shrink, sometimes if I get the label to grow the cell is still too small.
Can anyone help me to understand what I am doing wrong and show me how to get the constraints correct to get this to work?
Thank you
回答1:
- Set tableView.rowHeight = UITableViewAutomaticDimension
- Remove this function in your code func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
- Set height constraint of LabelBio greaterOrEqualThan 0
回答2:
here are the steps ...
hope you know about
autylayout
. use constraints (left to content view
,right to content view
,bottom to content view
andverticle to your first label
)then in your
viewdidLoad
method addYourTableView.estimatedRowHeight = 200
-> give any height you want.finally define below method :
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
you good to go.
来源:https://stackoverflow.com/questions/46716123/automatically-increase-decrease-uilabelview-height-in-uitableviewcell