iOS dynamical height of UITableViewCell and heightForRowAtIndexPath

前端 未结 3 528
太阳男子
太阳男子 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:46

    Calculate the height of the content dynamically using boundingRectWithSize. If you have a UILabel which is dynamic, you can use the following :

    - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
       /* Check Content Size and Set Height */
        CGRect answerFrame = [YOUR_LABEL.text boundingRectWithSize:CGSizeMake(240.f, CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName:[UIFont fontWithName:@"" size:14.0f]} context:nil];
    
        CGSize requiredSize = answerFrame.size;
    
        return requiredSize.height;
    }
    

提交回复
热议问题