2011-04-21 44 views
5

我工作的應用程序在Facebook上加載用戶的帖子。所有這些信息都放入tableview中。當你點擊它時,這些單元是可消耗的。現在只有文字。我計算文字的高度,以便知道細胞在展開時需要多高。基於內容的動態單元格高度ios

問題是我所需要的不僅僅是文本。如果用戶在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預先

+0

其實..這很簡單。 只需在height中添加以下內容ForRowAtIndexPath NSString * video = [fbSource objectAtIndex:indexPath.row]; (視頻!= @「空」){ return labelSize.height + 136; } return labelSize.height + 36; – Jos 2011-04-21 18:52:12

+2

您應該將其作爲答案發布,然後接受它,因此此問題不再顯示爲未答覆。 – Anomie 2011-04-21 19:14:25

+0

是的,那是不可能的。我需要等待8個小時,但現在就做到了! – Jos 2011-04-22 15:21:37

回答

3

其實..它很簡單。只需要在heightForRowAtIndexPath中添加以下代碼

NSString *video = [fbSource objectAtIndex:indexPath.row] ; 
    if (video != @"empty") { return labelSize.height + 136; } 
    return labelSize.height + 36; 
相關問題