简体   繁体   English

隐藏视图在旋转时在自定义 UITableViewCell 中可见

[英]Hidden view is visible in custom UITableViewCell on rotate

I have a UITableView with a custom UITableViewCell implemented as a NIB.我有一个 UITableView 和一个作为 NIB 实现的自定义 UITableViewCell。 The custom cell includes a UILabel and UIView (ala new email / message indicator).自定义单元格包括 UILabel 和 UIView(又是新的 email / 消息指示器)。 In cellForRowAt I set the lable to some text and hide the UIView but setting its isHidden property to false.cellForRowAt中,我将标签设置为一些文本并隐藏 UIView 但将其isHidden属性设置为 false。 The result is a list of rows with only a line of text as expected.结果是一个行列表,其中只有一行文本。 However if I were to rotate the screen to landscape and then back to portrait, several of the rows that were not visible in landscape but now are in portrait have the UIView visible.但是,如果我将屏幕旋转到横向,然后再转回纵向,则在横向中不可见但现在纵向显示的几行 UIView 可见。 If these rows are scrolled off the view and back into it the UIView is hidden as expected.如果这些行从视图中滚动并返回到视图中,则 UIView 将按预期隐藏。 I can recreate this issue in a very simple sample where我可以在一个非常简单的示例中重新创建此问题,其中

UIViewController UITableView leading=0, trailing=0, top=0, bottom=0 UIViewController UITableView 前导=0,尾随=0,顶部=0,底部=0

UITableViewCell UIStackView - axis=horizontal, alignment=center, distribution=fill, leading=0, trailing=0, top=0, bottom=0 UILabel UIView - width=10, height=10 UITableViewCell UIStackView - 轴=水平,对齐=中心,分布=填充,前导=0,尾随=0,顶部=0,底部=0 UILabel UIView - 宽度=10,高度=10

Any thoughts on the cause of this behaviour?对这种行为的原因有任何想法吗? I have tried setting cell.layoutIfNeeded() in cellForRowAt with no success.我尝试在cellForRowAt中设置cell.layoutIfNeeded() ,但没有成功。

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

As requested cellForRowAt code attached.根据要求附上cellForRowAt代码。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell

    cell.titleLabel.text = dataSource[indexPath.row]
    cell.indicatorView.isHidden = true

    return cell
}

Its reusability issue.... in your TableViewCell override... prepareToReuse() .. and set default value there...它的可重用性问题....在您的TableViewCell覆盖... prepareToReuse() .. 并在那里设置默认值...

func prepareForReuse()

If a UITableViewCell object is reusable—that is, it has a reuse identifier—this method is invoked just before the object is returned from the UITableView method dequeueReusableCell(withIdentifier:).如果一个 UITableViewCell object 是可重用的——也就是说,它有一个重用标识符——这个方法在 object 从 UITableView 方法返回之前调用。 For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state.出于性能原因,您应该只重置与内容无关的单元格属性,例如 alpha、编辑和选择 state。 The table view'€™s delegate in tableView(_:cellForRowAt:) should always reset all content when reusing a cell.在重用单元格时,tableView(_:cellForRowAt:) 中的表视图委托应始终重置所有内容。 If the cell object does not have an associated reuse identifier, this method is not called.如果单元格 object 没有关联的重用标识符,则不调用此方法。 If you override this method, you must be sure to invoke the superclass implementation如果重写此方法,则必须确保调用超类实现

Apple docs 苹果文档

hide indicator view in storyboard too在 storyboard 中隐藏指示器视图

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM