2013-02-19 147 views
19

所以,heightForRowAtIndexPath似乎在cellForRowAtIndexPath之前被调用。我目前使用cellForRowAtIndexPath来计算每个单元格的高度(它们是可变高度,使用UILabel以及不同数量的文本)。在cellForRowAtIndexPath中调用heightForRowAtIndexPath?

我很困惑,至于如何正确设置单元格的高度如果高度的方法之前调用该方法计算它。

我创建的NSString类别,另一个UILabel,这共同努力使UILabel大小合适。这个问题是包含这些标签的UITableViewCell实例没有被调整大小,所以标签挂在每个标签的末尾,重叠在下面的标签上。

我知道我需要做的是使单元格的高度与标签的高度相等,再加上一些额外的空白空间,但我很疑惑我如何在标签高度设置之前设置高度已计算。我可以在不使用heightForRowAtIndexPath的情况下设置某个人的身高吗?如果不是,我可以计算其他地方的细胞高度吗?我目前使用indexPath.row来实现这一点,所以事先做好会很棘手。下面的代码我的cellForRowAtIndexPath:

的cellForRowAtIndexPath

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath 
{ 
    FQCustomCell *_customCell = [tableView dequeueReusableCellWithIdentifier: @"CustomCell"]; 
    if (_customCell == nil) { 
     _customCell = [[FQCustomCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"CustomCell"]; 
    } 

    _customCell.myLabel.text = [[_loadedNames objectAtIndex: indexPath.row] name]; 
    _customCell.myLabel.font = [UIFont fontWithName: @"FontA" size: 15.0f]; 

    UIView *backView = [[UIView alloc] initWithFrame: CGRectZero]; 
    backView.backgroundColor = [UIColor clearColor]; 
    _customCell.backgroundView = backView; 

    _customCell.myLabel.frame = CGRectMake(5, 5, 265, 65); 

    [_customCell.myLabel sizeToFitMultipleLines]; 
    return _customCell; 
} 
+0

从iOS 8开始,您还可以使用自动计算的动态行高。 HTTP://计算器。com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – Suragch 2016-04-12 06:21:00

回答

15

你需要计算heightForRowAtIndexPath细胞的高度,像

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *theText=[[_loadedNames objectAtIndex: indexPath.row] name]; 
    CGSize labelSize = [theText sizeWithFont:[UIFont fontWithName: @"FontA" size: 15.0f] constrainedToSize:kLabelFrameMaxSize]; 
    return kHeightWithoutLabel+labelSize.height; 
} 

您可以在标签尺寸设置一些限制通过设定kLabelFrameMaxSize

#define kLabelFrameMaxSize CGSizeMake(265.0, 200.0) 

然后,通过添加具有变量标签高度的常量高度来返回高度。

此外,为了保持一致,你应该使用同样的方法设置帧cellForRowAtIndexPath而不是使用sizeToFitMultipleLines

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath 
{ 
    FQCustomCell *_customCell = [tableView dequeueReusableCellWithIdentifier: @"CustomCell"]; 
    if (_customCell == nil) { 
     _customCell = [[FQCustomCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"CustomCell"]; 
    } 

    _customCell.myLabel.text = [[_loadedNames objectAtIndex: indexPath.row] name]; 
    _customCell.myLabel.font = [UIFont fontWithName: @"FontA" size: 15.0f]; 

    UIView *backView = [[UIView alloc] initWithFrame: CGRectZero]; 
    backView.backgroundColor = [UIColor clearColor]; 
    _customCell.backgroundView = backView; 

    CGSize labelSize = [_customCell.myLabel.text sizeWithFont:_customCell.myLabel.font constrainedToSize:kLabelFrameMaxSize]; 
    _customCell.myLabel.frame = CGRectMake(5, 5, labelSize.width, labelSize.height); 

    return _customCell; 
} 
+0

完美,谢谢! :) – Luke 2013-02-19 16:12:54

-1

对于每一个细胞heightforRowAtIndexPath被称为先的cellForRowAtIndexPath。因此,您可以先计算高度,然后为每个单元填充CellForRowAtIndexPath中的数据。

+5

你刚刚说了我在问题中所说的话。 – Luke 2013-02-19 11:51:35

1

您应该在heightForRowAtIndexPath方法中计算单元格的高度。应该在不使用任何UILabel的情况下完成,就像JP Hribovsek的答案中的heightForRowAtIndexPath实现一样。

然后在cellForRowAtIndexPath不要设置任何子视图的框架,也不要调用标签的sizeToFitMultipleLines方法。相反,实施layoutSubviews方法FQCustomCell,使子视图帧设置正确的单元格的任何可能的界限。你不应该关心这种方法中的文本或字体大小,只要确保标签边距是正确的。如果在heightForRowAtIndexPath方法中单元格的高度计算正确,则标签的大小将恰到好处。

相关问题