Reputation: 2433
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;
}
Upvotes: 1
Views: 5619
Reputation: 17378
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
Upvotes: 1