IOS dynamic height UITableViewCell and heightForRowAtIndexPath

I am using Autolayout for my new UITableViewCells in a large project.

I have one TableView, where the height of each row is calculated automatically, there I do not use the delegate function heightForRowAtIndexPath.

I declared the estimated line height:

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

My question is: there is a lot of UITableViewCells on another TableViewController where I have to programmatically declare the cell height at heightForRowAtIndexPath. I know that it would be better to convert the whole cell to use a unique solution, but there are a lot of different cells in this project, so I would like to use a workaround and combine dynamically calculated height with autostart and programmed line height.

Is it possible?

+4
3

iOS 8 , . . IOS 8, .

IOS 8:

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

:

tableView.estimatedRowHeight = 400.0
tableView.rowHeight = UITableViewAutomaticDimension

estimatedRowHeight , .

+15

boundingRectWithSize. UILabel, , :

- (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;
}
+1

.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
     int topPadding = cell.yourLabel.frame.origin.x;
     int bottomPadding = cell.frame.size.heigth-(topPadding+cell.yourLabel.frame.size.height);
     NSString *text = [DescArr objectAtIndex:[indexPath row]];
     CGSize maximumSize = CGSizeMake(cell.yourLabel.frame.size.width, 9999);
     CGSize expectedSize = [text sizeWithFont:yourCell.yourLabel.font constrainedToSize:maximumSize lineBreakMode:yourCell.yourLabel.lineBreakMode];

     return topPadding+expectedSize.height+bottomPadding;
}
0

All Articles