本文介绍了dequeued UITableViewCell具有不正确的布局,直到滚动(使用autolayout)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的 UITableViewCell 子类,它已在Interface Builder中对其应用了自动布局约束。单元格包含多个视图,包括 UITextField

I have a custom UITableViewCell subclass which has had autolayout constraints applied to it in Interface Builder. The cell contains multiple views, including a UITextField.

相关地,的大小UITextField 受约束,使得它与下一个视图之间存在默认的水平间距。

Relevantly, the size of the UITextField is constrained such that there is default horizontal spacing between it and the next view.

单元格实例化如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ProgressCell";
    ProgressCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
                                                     forIndexPath:indexPath]

    cell.textField.text = @"Some string that is different for each cell";

    return cell;
}

当单元格首次出现时, UITextField 超出正确的框架,并显示在右边的 UIView 后面。但是,当我在屏幕上滚动单元格,暂停,然后向后滚动时,文本会被正确截断。

When the cell first appears, the UITextField overruns the correct frame, and appears behind the UIView to its right. However, when I scroll the cell off screen, pause, and then scroll back, the text is truncated correctly.

下面显示了一个示例(在第二次编辑时)。

An example is shown below (at the second edit).

我试过调用 [cell setNeedsLayout] [cell setNeedsDisplay] 用于 cellForRowAtIndexPath 中的单元格,以及延迟后执行它们。两者都没有效果。

I have tried calling [cell setNeedsLayout] and [cell setNeedsDisplay] for the cell in cellForRowAtIndexPath, as well as performing them after a delay. Neither is effective.

什么是滚动屏幕,这样做会导致单元格正确显示,我该如何复制或修复底层问题?

What is scrolling off screen doing that is causing the cell to appear correctly, and how can I either replicate this or fix the underlying issue?

编辑:

致电

[self.tableView reloadData];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];

为了重新加载单元格,似乎会导致布局第一次正确显示。

in order to reload the cell, appears to cause the layout to appear correctly first time.

然而,它现在在滚动时(偶尔)中断(即,当向上滚动时,布局约束现在没有正确应用)。

However, it now breaks (occasionally) on scrolling (ie, when scrolling back up, the layout constraints are now not applied correctly).

cellForRowAtIndexPath 中调用 [cell setNeedsLayout] 似乎无法修复此问题问题。

Calling [cell setNeedsLayout] in cellForRowAtIndexPath appears not to fix this issue.

EDIT2:

顶部单元格,如图所示,正确显示(如底部单元格所示),直到我向下滚动屏幕。它已经消失了。

The top cell, as shown here, appeared correctly (as the bottom cell does) until I scrolled down the screen. It since disappeared.

这反映了第一次编辑时的问题 - 这是问题的第二次渲染(让我觉得它可能与重用有关)单元格?)

This reflects the problem as of the first edit - it's the second rendering that is the problem (makes me think that it might have something to do with reusing the cell?)

推荐答案

如果在 UITableViewCell 上命名属性子类 textLabel defaultTextLabel ,然后IB将忽略您指定的约束并使用默认约束覆盖它们,没有警告发布。

If you name a property on a UITableViewCell subclass textLabel or defaultTextLabel, then IB will ignore the constraints you have specified and override them with default ones, with no warnings issued.

即使是在IB中使用自定义样式设计的单元格也是如此,该样式没有可见 textLabel detailTextLabel 属性。

This is the case even on cells designed in IB with the Custom style, which have no visible textLabel or detailTextLabel properties.

如果添加 UIImageView类型的属性,也会发生这种情况

This also happen if add a property of type UIImageView property on a UITableViewCell subclass and name it imageView.

这篇关于dequeued UITableViewCell具有不正确的布局,直到滚动(使用autolayout)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 14:29