0

I have read alot about dynamic height for UITableViewCell and for the life of me I cannot get it working.

I have a uilabel with dynamic content within a dynamic cell.

I am using the storyboard and have the constraints as so :

enter image description here

I am populating the table using

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"labelCell";

    detailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[detailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.dynamicLabel.text = sectionString;

    return cell;
}

I do not know why everything I have tried has failed. I am thinking it may be the connections on the uiLabel?

2
  • 1
    I believe you still need to calculate the height individually for each cell at heightforrowatindexpath.
    – A-Live
    May 2, 2014 at 11:43
  • Try heightForRowAtIndexPath: method of UITableView. And if you need to change the height of UILabel according to cellHeight, add Bottom space of UILabel to container
    – Akhilrajtr
    May 2, 2014 at 11:43

1 Answer 1

0

This is it , Right :

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        NSString *text = myText;// [myArray objectAtIndex:indexPath.row];
        CGSize size;
        if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {
            // code here for iOS 5.0,6.0 and so on
            CGSize fontSize = [text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:17]];
            size = fontSize;
        }
        else {
            // code here for iOS 7.0
            NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                                  [UIFont fontWithName:@"Helvetica Neue" size:19], NSFontAttributeName,
                                                  nil];
            CGRect fontSizeFor7 = [text boundingRectWithSize:CGSizeMake(571, 500)
                                                     options:NSStringDrawingUsesLineFragmentOrigin
                                                  attributes:attributesDictionary
                                                     context:nil];
            size = fontSizeFor7.size;

        }
        NSLog(@"indexPAth %ld %f",(long)indexPath.section, size.height);

        return size.height +30 ;

    }

Finally, at the delegate of cellForRowAtIndexPath: don't forget to make the UILabel flexible for text :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // ........
    cell.dynamicLabel.text = sectionString;
     cell.dynamicLabel.numberOfLines = 0;

    return cell;

}
2
  • hi @kumar KL . This approach works howwver seems to have a max size. Can I confirm a few things? Why is +30 added to the height at the end? And is the boudingRectWithSize limiting it to a max height? May 2, 2014 at 13:06
  • +30 is just for spacing between text and Cell, you can change it to your need . And boudingRectWithSize - its not a constant value, change according to your label
    – Kumar KL
    May 5, 2014 at 4:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.