【问题标题】:Changing custom cell's height dynamically not working动态更改自定义单元格的高度不起作用
【发布时间】:2013-03-09 20:37:05
【问题描述】:

我想根据标签的高度动态设置单元格的高度。

我创建了单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"ORDStepsCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

UILabel *stepsLabel;
UILabel *stepsNumberLabel;
UIImageView *bulletImageView;
UIImageView *cellBackgroungImage;

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:simpleTableIdentifier];

    CGRect stepsLabelFrame = CGRectMake(20, 0, 253, 21);
    CGRect bulletImageViewFrame = CGRectMake(281, 25, 29, 29);;
    CGRect stepsNumberLabelFrame = CGRectMake(3, 3, 21, 21);
    CGRect cellBackgroungImageFrame = CGRectMake(0, 0, 320, 80);

    cellBackgroungImage = [[UIImageView alloc] initWithFrame:cellBackgroungImageFrame];
    cellBackgroungImage.image = [UIImage imageNamed:@"list-item"];

    stepsLabel = [[UILabel alloc] initWithFrame:stepsLabelFrame];
    stepsLabel.textAlignment = UITextAlignmentRight;
    stepsLabel.tag = 0;

    bulletImageView = [[UIImageView alloc] initWithFrame:bulletImageViewFrame];
    bulletImageView.image = [UIImage imageNamed:@"step-bullet"];

    stepsNumberLabel = [[UILabel alloc] initWithFrame:stepsNumberLabelFrame];
    stepsNumberLabel.textAlignment = UITextAlignmentCenter;
    stepsNumberLabel.textColor = [UIColor whiteColor];
    stepsNumberLabel.backgroundColor = [UIColor clearColor];
    stepsNumberLabel.font = [UIFont boldSystemFontOfSize:17.0];
    stepsNumberLabel.tag = 1;

    [cell.contentView addSubview:cellBackgroungImage];
    [cell.contentView addSubview:stepsLabel];
    [cell.contentView addSubview:bulletImageView];
    [bulletImageView addSubview:stepsNumberLabel];

} else {
    stepsLabel = (UILabel *)[cell viewWithTag:0];
    stepsNumberLabel = (UILabel *)[cell viewWithTag:1];
}

stepsLabel.text = [preperationSteps objectAtIndex:indexPath.row];
[stepsLabel setBackgroundColor:[UIColor redColor]];
[stepsLabel setNumberOfLines:0];
[stepsLabel sizeToFit];

NSLog(@"cellHeight = %f", cell.frame.size.height);

stepsLabel.frame = CGRectMake(20, (cell.frame.size.height - stepsLabel.frame.size.height) / 2, 253, stepsLabel.frame.size.height);

stepsNumberLabel.text = [NSString stringWithFormat:@"%d", indexPath.row + 1];

[cell setSelected:NO];
[cell setSelectionStyle:UITableViewCellEditingStyleNone];

return cell;

}

然后我计算了标签的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 253, 30)];
NSString *labelText = [preperationSteps objectAtIndex:indexPath.row];
cellLabel.text = labelText;
[cellLabel setNumberOfLines:0];

[cellLabel sizeToFit];

cellLabel.frame = CGRectMake(0, 0, 253, cellLabel.frame.size.height);
CGFloat cellLabelHeight = cellLabel.frame.size.height;
NSLog(@"cellLabelHeight = %f", cellLabelHeight + 20);
return cellLabelHeight + 20;
}

在日志中:

2013-03-08 17:37:18.161 Recipe[37547:c07] cellLabelHeight = 125.000000
2013-03-08 17:37:18.161 Recipe[37547:c07] cellLabelHeight = 62.000000
2013-03-08 17:37:18.161 Recipe[37547:c07] cellLabelHeight = 41.000000
2013-03-08 17:37:18.162 Recipe[37547:c07] cellLabelHeight = 41.000000
2013-03-08 17:37:18.162 Recipe[37547:c07] cellLabelHeight = 41.000000
2013-03-08 17:37:18.162 Recipe[37547:c07] cellLabelHeight = 41.000000
2013-03-08 17:37:18.163 Recipe[37547:c07] cellLabelHeight = 62.000000
2013-03-08 17:39:34.852 Recipe[37581:c07] cellHeight = 44.000000
2013-03-08 17:39:34.852 Recipe[37581:c07] cellHeight = 44.000000
2013-03-08 17:39:34.853 Recipe[37581:c07] cellHeight = 44.000000
2013-03-08 17:39:34.853 Recipe[37581:c07] cellHeight = 44.000000
2013-03-08 17:39:34.854 Recipe[37581:c07] cellHeight = 44.000000
2013-03-08 17:39:34.854 Recipe[37581:c07] cellHeight = 44.000000
2013-03-08 17:39:34.855 Recipe[37581:c07] cellHeight = 44.000000

正如您所见,即使从 heightForRowAtIndexPath 返回的值不同,单元格的高度也是固定的 44。 我知道在 cellForRowAtIndexPath 之前调用了 heightForRowAtIndexPath(从日志中可以看出),那么如何更改高度?

【问题讨论】:

  • 在对cellForRowAtIndexPath 的调用中未设置单元格的高度。该方法仅返回具有标准高度的单元格。表格获取单元格并将其添加到表格后,将设置单元格的高度。所以在cellForRowAtIndexPath 中记录高度是有误导性的。只需确保当单元格的高度最终设置为全高时,您的单元格的自定义内容会正确调整自身。
  • 如果你使用下面的,会发生什么? - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [indexPath row]; }
  • 我可以看到里面有一个单元格和分隔符。
  • 我只是说如果行没有随着上面的代码逐渐增加,那么他在其他地方有问题。这就是为什么我说“会发生什么?”
  • 如果 cellForRowAtIndexPath 中的高度是默认高度,我需要在哪里调整单元格的子视图?在 willDisplayCell 中?

标签: objective-c cocoa-touch uitableview height


【解决方案1】:

您在heightForRowAtIndexPath 中给出的高度就是要显示的高度。将此代码粘贴到您的控制器中,然后查看您实际显示的高度。

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%f", cell.frame.size.height);
}

您登录cellForRowAtIndexPath 的高度只是单元格的默认高度。在调用cellForRowAtIndexPath 之后分配高度。

【讨论】:

  • 它确实显示了正确的高度。第一个单元格显示正确(除了我尝试使用可拉伸图像设置的背景),但其他单元格彼此重叠,隐藏了标签。那么如果 cellForRowAtIndexPath 中的单元格高度不是所需的高度,我需要在哪里计算标签的框架?
  • 从我在您的代码中可以看到,您已将背景图像的高度硬编码为 80。由于背景图像视图的高度取决于单元格的高度,因此您必须使用您为避免重叠而计算的单元格。您还可以使用willDisplayCell 方法来访问单元格的可变高度,以进行 imageView 的框架计算。
【解决方案2】:

我想根据标签的高度动态设置单元格的高度。

您可能想要这样,但您没有在 -tableView:cellForRowAtIndexPath: 方法中的任何位置设置单元格高度。 -tableView:heightForRowAtIndexPath: 不会为您设置单元格的高度——当您在前一种方法中配置单元格时,您需要这样做。后一种方法的原因是桌子可以确定自己的高度。每当它重新加载其数据时,它都会向代理询问每一行的高度,并使用该信息来计算表格的总高度。每次为给定行提供单元格时,您仍然需要设置单元格高度。

【讨论】:

    【解决方案3】:

    为我解决这个问题的方法是在 cellForRowAtIndexPathheightForRowAtIndexPath 方法中使用 sizeWithFont:constrainedToSize:lineBreakMode: 计算标签的文本长度,然后使用计算的大小在 cellForRowAtIndexPath 中设置标签的框架。
    如果它对任何人有任何帮助,我会发布代码。

    【讨论】:

      猜你喜欢
      • 2021-08-09
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多