UITableViewCell 的动态高度 [英] Dynamic Height of UITableViewCell
问题描述
好的..所以在将其标记为重复问题之前,请阅读它.我已经实现了 CIMGF 给出的代码,用于动态调整 UITableViewCell
的大小.它第一次起作用,但现在似乎不起作用.很奇怪吧?无论如何,这里是代码片段:
Okay..so before you mark this is as a repeated question, read it. I've implemented the code given by CIMGF for dynamic resizing of UITableViewCell
. It worked for the first time around but does not seem to work now. Strange, right?
Anyway, here are the code snippets:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [[buzzList objectAtIndex:[indexPath row]] objectForKey:@"description"];
CGSize constraint = CGSizeMake(260, 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = size.height;
return height +25 +(CELL_CONTENT_MARGIN * 2);
}
和cellForRowAtIndexPath
对应的代码是:
[cell.descriptionLabel setLineBreakMode:UILineBreakModeWordWrap];
[cell.descriptionLabel setMinimumFontSize:FONT_SIZE];
[cell.descriptionLabel setNumberOfLines:0];
[cell.descriptionLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [[[buzzList objectAtIndex:indexPath.row] objectForKey:@"description"] sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
cell.descriptionLabel.text=[[buzzList objectAtIndex:indexPath.row] objectForKey:@"description"];
CGRect rect=CGRectMake(45, 20, 260, size.height);
[cell.descriptionLabel setFrame:rect];
值 45,20,260 由我设置以获取单元格中文本起点的确切位置及其宽度.这些值是正确的,不需要修改.但是,当我执行代码时,我获得了正确的单元格高度(根据文本动态调整)BUT
descriptionLabel
最多显示 2 行文本.之后它会截断文本.:(
Where the values 45,20,260 are set by me to get the exact location of starting point of text in the cell and it's width. The values are correct and need no modification. However, When I execute the code, I get the right height for the cell (Dynamically adjustable according to the text) BUT
the descriptionLabel
shows maximum of 2 lines text. It truncates the text after that. :(
推荐答案
您对描述标签的约束包含两个常量 CELL_CONTENT_WIDTH
和 CELL_CONTENT_MARGIN
,而 CGRect
您正在创建包含一个数字 (260).如果 CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2)
> 260 那么这应该是你的问题.
Your constraint for the description label contains two constants CELL_CONTENT_WIDTH
and CELL_CONTENT_MARGIN
, whereas the CGRect
you are creating contains a number (260). If CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2)
> 260 then this should be your problem.
这篇关于UITableViewCell 的动态高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!