我正在开发一款加载 Facebook 用户帖子的应用程序。所有这些信息都放在一个 TableView 中。当你点击它时,细胞是消耗性的。现在里面只有文字。我计算文本的高度,这样我就知道展开时单元格需要多高。
我遇到的问题是其中需要的不仅仅是文本。如果用户在 facebook 上发布视频,该视频也将嵌入到单元格中。但是由于高度是根据文本计算的,因此没有为嵌入的视频留出空间。我可以在单元格中添加边距,但此边距将应用于所有单元格。
这是我现在使用的代码:
//Function executes everything within when a certain row in tapped/selected
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView reloadData];
[self stopLoading];
if (selectedIndexPath == indexPath)
{
selectedIndexPath = nil;
}
else
{
selectedIndexPath = indexPath;
}
[self.tableView deselectRowAtIndexPath : indexPath animated : YES];
[tableView beginUpdates];
[tableView endUpdates];
}
//Change heigth of the selected row
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ((selectedIndexPath != nil) && (selectedIndexPath.row == indexPath.row))
{
labelSize = [[result2 objectAtIndex:indexPath.row] sizeWithFont:[UIFont fontWithName:@"Helvetica" size:13.0]
constrainedToSize:CGSizeMake(220.0f, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 136;
}
return 68.0;
}
所以我需要知道什么。如何根据所有内容计算单元格的高度?而不仅仅是文本。
如果有人可以提供帮助,那就太好了!
提前致谢
最佳答案
其实……很简单。只需在 heightForRowAtIndexPath 中添加以下内容
NSString *video = [fbSource objectAtIndex:indexPath.row] ;
if (video != @"empty") { return labelSize.height + 136; }
return labelSize.height + 36;
关于iphone - 基于内容 ios 的动态单元格高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5748309/