UITableViewCell需要reloadData()来调整大小以正确高度 [英] UITableViewCell needs reloadData() to resize to correct height

查看:127
本文介绍了UITableViewCell需要reloadData()来调整大小以正确高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有应用约束的自定义tableview单元格,但是第一次显示表格时行高度没有正确调整大小,除非创建新单元格,有没有办法在不重新调用reloadData的情况下执行此操作?

I have the a custom tableview cell with applied constraints but the first time the table is displayed the row height is not resized properly unless new cells are created, is there a way to do this without calling reloadData again?

推荐答案

是的。这实际上是一个自我调整的问题,你需要解决它,直到它被修复。

Yes. This is actually an issue with self-sizing that you need to work around until it is fixed.

问题是当实例化一个单元格时,它的初始宽度是基于的在故事板上的宽度。由于这与 tableView 宽度不同,因此初始布局错误地确定了内容实际需要的行数。

The problem is that when a cell is instantiated, its initial width is based on the storyboard width. Since this is different from the tableView width, the initial layout incorrectly determines how many lines the content actually would require.

这就是为什么内容第一次没有正确调整大小,但是一旦你(重新加载数据)或者在屏幕外滚动单元格,然后在屏幕上正确显示。

This is why the content isn't sized properly the first time, but appears correctly once you (reload the data, or) scroll the cell off-screen, then on-screen.

您可以通过确保单元格的宽度与 tableView 宽度匹配来解决此问题。您的初始布局将是正确的,无需重新加载tableView:

You can work around this by ensuring the cell's width matches the tableView width. Your initial layout will then be correct, eliminating the need to reload the tableView:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    [cell adjustSizeToMatchWidth:CGRectGetWidth(self.tableView.frame)];

    [self configureCell:cell forRowAtIndexPath:indexPath];

    return cell;
}

在TableViewCell.m中:

In TableViewCell.m:

- (void)adjustSizeToMatchWidth:(CGFloat)width
{
    // Workaround for visible cells not laid out properly since their layout was
    // based on a different (initial) width from the tableView.

    CGRect rect = self.frame;
    rect.size.width = width;
    self.frame = rect;

    // Workaround for initial cell height less than auto layout required height.

    rect = self.contentView.bounds;
    rect.size.height = 99999.0;
    rect.size.width = 99999.0;
    self.contentView.bounds = rect;
}

我还建议查看smileyborg的关于自我调整单元格的优秀答案,以及他的示例代码。当我碰到你遇到的同样问题时,这就是解决方案的原因。

I'd also recommend checking out smileyborg's excellent answer about self-sizing cells, along with his sample code. It's what tipped me off to the solution, when I bumped into the same issue you are having.

更新:

configureCell:forRowAtIndexPath:是Apple在其示例代码中使用的方法。当你有多个 tableViewController 时,通常将它子类化,并打破特定于控制器的 cellForRowAtIndexPath:每个视图控制器中的代码。超类处理公共代码(例如出列单元格)然后调用子类,以便它可以配置单元的视图(从控制器到控制器会有所不同)。如果您不使用子类,只需将该行替换为特定代码即可设置您的单元格(自定义)属性:

configureCell:forRowAtIndexPath: is an approach Apple uses in its sample code. When you have more than one tableViewController, it is common to subclass it, and break out the controller-specific cellForRowAtIndexPath: code within each view controller. The superclass handles the common code (such as dequeuing cells) then calls the subclass so it can configure the cell's views (which would vary from controller to controller). If you're not using subclassing, just replace that line with the specific code to set your cell's (custom) properties:

    cell.textLabel.text = ...;
    cell.detailTextLabel.text = ...;

这篇关于UITableViewCell需要reloadData()来调整大小以正确高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆