1

I am able to display the cell content in the table and also Wordwrap it to display the message entirely. But I noticed that if the content is more than the height than it becomes clumpsy. How do I increase the heighten a cell dynamically if the content is too large. Since I am retrieving data from a url the length varies so I do not want to hard code the height but want to vary it as per the content length.

The code that I have tried so far is as under :

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *person = [myPeople objectAtIndex:[indexPath row]]; 

    NSString *personName = [person valueForKey:@"text"];

    NSString *cellText    = personName;
    UIFont *cellFont      = [UIFont fontWithName:@"Helvetica-neuve" size:21.0];
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    CGSize labelSize      = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
    int buffer  = 70;
    return labelSize.height + buffer;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";  

    UITableViewCell *cell = [commentView dequeueReusableCellWithIdentifier:CellIdentifier];  
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 6;
        cell.textLabel.font          = [UIFont fontWithName:@"Helvetica-neuve" size:21.0];
        [cell.textLabel setMinimumFontSize:13.0];
        [cell.textLabel setAdjustsFontSizeToFitWidth:NO];
    } 
    NSDictionary *person = [myPeople objectAtIndex:[indexPath row]]; 

    NSString *personName = [person valueForKey:@"text"];
    cell.textLabel.text = personName;
    return cell;
    }

1 Answer 1

1

Much as I dislike posting just a link. This will show you how to calculate the correct height for your cell on the fly.

Adjust UILabel height depending on the text

Sign up to request clarification or add additional context in comments.

2 Comments

But I want to adjust cell height
Sorry, my mistake. I see you are already using that method. What happens when you hook willDrawCell with the same approach.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.