Automatically increase/decrease UILabelView Height in UITableViewCell?

被刻印的时光 ゝ 提交于 2019-12-20 05:56:07

问题


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:


  1. Set tableView.rowHeight = UITableViewAutomaticDimension
  2. Remove this function in your code func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
  3. 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 and verticle to your first label)

  • then in your viewdidLoad method add YourTableView.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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!