iOS dynamical height of UITableViewCell and heightForRowAtIndexPath

前端 未结 3 527
太阳男子
太阳男子 2021-01-04 23:24

I\'m using Autolayout for my new UITableViewCells in a large project.

I\'ve one TableView where the height of each row is calculated automatically, there I don\'t us

3条回答
  •  清歌不尽
    2021-01-04 23:37

    If you are using iOS 8 and above, you do not need to calculate height dynamically. Auto layout will do all for you. But if you are using lower than IOS 8, you need to calculate cell height.

    For IOS 8:

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

    And add below code in your controller:

    tableView.estimatedRowHeight = 400.0
    tableView.rowHeight = UITableViewAutomaticDimension
    

    Where estimatedRowHeight should be max height which can be for your cell.

    Thanks

提交回复
热议问题