动态调整tableView单元格的高度 [英] dynamically size the height of the tableView cells
问题描述
我能够在表格中显示单元格内容,也可以自动换行以完全显示消息.但是我注意到,如果内容大于高度,则变得笨拙.如果内容太大,如何动态增加单元格的高度.由于我要从url检索数据,因此长度是变化的,所以我不想对高度进行硬编码,而是希望根据内容长度来变化它.
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;
}
推荐答案
就像我不喜欢发布链接一样.这将向您展示如何为飞行中的单元格计算正确的高度.
Much as I dislike posting just a link. This will show you how to calculate the correct height for your cell on the fly.
这篇关于动态调整tableView单元格的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!