2013-04-10 62 views
8

我有一个静态单元格的表格。对于一个细胞,我想根据标签的高度(在细胞内)改变其高度,同时保持所有其他细胞高度不变。我如何获得当前细胞的高度?或者,也许有更好的方法?heightForRowAtIndexPath中的当前单元格高度?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if ([indexPath section] == 2) { 
     return self.myLabel.frame.origin.y *2 + self.myLabel.frame.size.height; 
    } else { 
     return ...; // what should go here, so the cell doesn't change its height? 
    } 
} 
+3

您单元的默认高度去那里。 44如果你没有改变任何东西。 – Desdenova 2013-04-10 13:50:58

+0

您还可以使用UIKIt对NSString方法'sizeWithFont:'的扩展来计算标签的单元实际所需高度。 – 2013-04-10 13:56:23

回答

12

您可以拨打:

[super tableView:tableView heightForRowAtIndexPath:indexPath] 

在else块,所以你不必担心,如果你永远改变了默认的高度。

4

可以获取/设置默认的高度tableView.rowHeight,或者您也可以更改单元格高度前,你的身高存储,这样你就可以得到一些变量默认高度;

1

请做一个

if ([indexPath section] == 2) 
{ 
    if(indexPath.row == 1) 
     return self.myLabel.frame.origin.y *2 + self.myLabel.frame.size.height; 
    else 
     tableView.rowHeight 
} 
0

你,也许,要计算在约束宽度标签的高度。在这种情况下,你可以像创建方法:

- (CGFloat)textHeightOfMyLabelText { 
    CGFloat textHeight = [self.myLabel.text sizeWithFont:self.myLabel.font constrainedToSize:LABEL_MAX_SIZE lineBreakMode:self.myLabel.lineBreakMode].height; 
    return textHeight; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath使用的结果。 不要忘记添加标签的保证金值。

+0

是的,thx,我只是要改变它=) – Ossir 2013-04-10 13:59:53

0
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == youSectionNumber) 
    { 
     if (indexPath.row == numberOfrow) 
     { 
      return self.myLabel.frame.origin.y *2 + self.myLabel.frame.size.height; 
     } 
    } 

     return 44; // it is default height of cell; 
} 
1

@talnicolas伟大的答案,以下是雨燕3:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    if indexPath.section == 2 { 
     let easy = self.myLabel.frame 
     return easy.origin.y *2 + easy.size.height 
    } else { 
     return super.tableView(tableView, heightForRowAt: indexPath) 
    } 
} 
相关问题