iOS8: Possible to use “tableview.rowHeight = UITableViewAutomaticDimension” for static cells?

前端 未结 8 985
慢半拍i
慢半拍i 2020-12-14 17:00

I have a UITableView that has five static cells. I need the cell height of one cell to adjust automatically to its contents, which is one UILabel.

Is there any way I

8条回答
  •  北海茫月
    2020-12-14 17:35

    Expanding over Victors' answer, Static Cells based table views seem to autosize cells based on content and constraints once the following delegates are implemented:

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return UITableViewAutomaticDimension;
    }
    
    -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 44;
    }
    

    EXCEPT for when there are labels. For some reason the label intrinsic content heights do not seem to contribute towards the height calculation of the cell when they have . My current work around for this is to nest the labels in an UIView.

    Do the following:

    1. Embed the label (or labels) in a view. (Select label(s) on IB and hit Menu > Editor > Embed In > View)
    2. Establish horizontal and vertical margin constraints between this view and the cell
    3. Establish horizontal and vertical margin constrains between your label(s) and this view.

    Certainly feels like a hack but works for me in all the cases I have tried.

提交回复
热议问题