基于内容ios的动态单元高度

时间:2011-04-21 18:35:13

标签: iphone objective-c ios uitableview

我正在开发一个在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;
}  

所以我需要知道。如何根据内容计算单元格的高度?而不仅仅是文本。

如果有人能帮忙解决这个问题会很棒!

Thnx提前

1 个答案:

答案 0 :(得分:3)

实际上......很简单。只需在heightForRowAtIndexPath

中添加以下内容即可
    NSString *video = [fbSource objectAtIndex:indexPath.row] ; 
    if (video != @"empty") { return labelSize.height + 136; } 
    return labelSize.height + 36;
相关问题