Reputation:
I have a TextView in the UITableViewCell
, and I want my height TableViewCell is auto according TextView height.
I had worked some code, but it display on the IOS 8 only, I want it can action on the IOS 7.
Please help me!
Thanks all.
Here is image my code, it work on the IOS 8 very good.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *messageText = [NSString stringWithFormat:@"%@", [DicMessagesData objectForKey:@"ContentMessage"]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
CGRect textRect = [messageText boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSParagraphStyleAttributeName: paragraphStyle.copy} context:nil];
return textRect.size.height + 30;
}
Upvotes: 0
Views: 278
Reputation: 55
you can use this code:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = @"your text";
CGSize maximumLabelSize = CGSizeMake(CELL_CONTENT_WIDTH, 9999);
UITextField *textField = [[UITextField alloc] init];
textField.font = [UIFont systemFontOfSize:18];
textField.text = text;
CGSize expectSize = [textField sizeThatFits:maximumTXTSize];
return expectSize.height + 40;
}
Upvotes: 0
Reputation: 1213
Try this. It works. This will give dynamic height for each tableview cell based on the content.
- (CGFloat)heightForText:(NSString *)bodyText
{
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:bodyText];
CGFloat width = 278;
CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
return rect.size.height+10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
lblComment.numberOfLines = 0;
lblComment.lineBreakMode = NSLineBreakByWordWrapping;
CGFloat rowHeight = [self heightForText:[[allCommentsArr objectAtIndex:indexPath.row]objectForKey:@"comment"]];
lblComment.frame = CGRectMake(25, 25, 278, rowHeight);
NSLog(@"rowHeight=%f", rowHeight);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat rowHgt=[self heightForText:[[allCommentsArr objectAtIndex:indexPath.row]objectForKey:@"comment"]];
NSLog(@"rowHgt=%f", rowHgt);
return rowHgt+25;
}
Upvotes: 0
Reputation: 109
A few months ago I faced the same problem. Thankfully I found a simple blog on Table View Cells With Varying Row Heights. It will solve your problem.
Upvotes: 0
Reputation: 11
the best method i have found :
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = @"your text";
CGSize maximumLabelSize = CGSizeMake(CELL_CONTENT_WIDTH, 9999);
UILabel *notesLabel = [[UILabel alloc] init];
notesLabel.font = [UIFont systemFontOfSize:18];
notesLabel.text = text;
notesLabel.numberOfLines = 0;
notesLabel.lineBreakMode = NSLineBreakByWordWrapping;
CGSize expectSize = [notesLabel sizeThatFits:maximumLabelSize];
return expectSize.height + 40;
}
Upvotes: 1