2012-11-19 57 views
-2

可能重複:
How to adjust the hieght of the cell table when loading from the custom cells?增加自定義類的單元格加載高度?

我有一個應用程序中,我加載從不同的自定義類object.It細胞正在fine.But我的問題是我增加了在這個子類上的文本視圖。我需要根據文本的內容來調整單元格的高度在表視圖view.`

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 0) 
    { 
     return 125; 
    } 
    else 
    { 
     return 245; 
    } 
} 

我現在所做的這樣。但我希望根據自定義類單元格中的文本視圖內容大小更改單元格高度。有誰能夠幫助我?

+0

可以調用的cellForRowAtIndexPath:這裏根據單元格的內容計算高度。 – Dave

+0

這很好,如果你回答我的問題 –

+0

@Dave沒有必要調用cellForRowAtIndexPath,只需抓住內容並計算高度。你的方式是創建一個沒有必要的額外單元格。 – rckoenes

回答

0

你可以做到這一點在簡單的方法只是看this

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell; 
    UILabel *label = nil; 

    cell = [tv dequeueReusableCellWithIdentifier:@"Cell"]; 
    if (cell == nil) 
    { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease]; 

    label = [[UILabel alloc] initWithFrame:CGRectZero]; 
    [label setLineBreakMode:UILineBreakModeWordWrap]; 
    [label setMinimumFontSize:FONT_SIZE]; 
    [label setNumberOfLines:0]; 
    [label setFont:[UIFont systemFontOfSize:FONT_SIZE]]; 
    [label setTag:1]; 

    [[cell contentView] addSubview:label]; 
    } 
} 

則u可以計算單元格高度和這裏一個完整的例子link

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    NSString *text = [items objectAtIndex:[indexPath row]]; 

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

    CGFloat height = MAX(size.height, 44.0f); 

    return height + (CELL_CONTENT_MARGIN * 2); 
} 
+0

wat你是指.......? –

+0

是否看到我添加的鏈接? – Omarj

+0

這裏的pbm是內容文本視圖在這裏不可用,它在自定義類中。 –

相關問題