好的..所以在将其标记为重复问题之前,请阅读它。我已经实现了 CIMGF 给出的代码,用于动态调整UITableViewCell
. 它第一次起作用,但现在似乎不起作用。奇怪,对吧?无论如何,这里是代码片段:
- (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 行文本。之后它会截断文本。:(