UITableViewCell with dynamic height iOS

前端 未结 12 2319
日久生厌
日久生厌 2020-11-27 17:54

I have implemented TableView with CustomCell in my app,

I want dynamic height of my UITableViewCell according to text length in UITableViewCell

12条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 18:38

    A'm looking for a long time how to determinate cell height properly, - looks like - it's a best solution, boundingRectWithSize and constrainedToSize often incorrectly calculated text height, you need to create UILabel than use sizeThatFits function, see below

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
        {
    
        UILabel  * label = [[UILabel alloc] initWithFrame:CGRectMake(8, 5, celllabelWidth, 9999)];
        label.numberOfLines=0;
        label.font = [UIFont fontWithName:fontName size:textSize];
        label.text = @"celllabelTextHere";
    
        CGSize maximumLabelSize = CGSizeMake(celllabelWidth, 9999);
        CGSize expectedSize = [label sizeThatFits:maximumLabelSize];
        return expectedSize.height;
    }
    

提交回复
热议问题