且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

以编程方式 UITableViewCell 动态高度

更新时间:2023-11-30 15:26:34

对于动态 tableViewCell 高度使用这个.

For dynamic tableViewCell height use this.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  CGSize labelHeight = [self heigtForCellwithString:yourLabel.text    withFont:yourLabel.font];
    return labelHeight.height; // the return height + your other view height
}

-(CGSize)heigtForCellwithString:(NSString *)stringValue withFont:(UIFont)font{
 CGSize constraint = CGSizeMake(300,9999); // Replace 300 with your label width
  NSDictionary *attributes = @{NSFontAttributeName: font};
  CGRect rect = [stringValue boundingRectWithSize:constraint
                                       options:         (NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                    attributes:attributes
                                       context:nil];
    return rect.size;

}