简体   繁体   中英

Hidden view is visible in custom UITableViewCell on rotate

I have a UITableView with a custom UITableViewCell implemented as a NIB. The custom cell includes a UILabel and UIView (ala new email / message indicator). In cellForRowAt I set the lable to some text and hide the UIView but setting its isHidden property to 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. If these rows are scrolled off the view and back into it the UIView is hidden as expected. I can recreate this issue in a very simple sample where

UIViewController UITableView leading=0, trailing=0, top=0, bottom=0

UITableViewCell UIStackView - axis=horizontal, alignment=center, distribution=fill, leading=0, trailing=0, top=0, bottom=0 UILabel UIView - width=10, height=10

Any thoughts on the cause of this behaviour? I have tried setting cell.layoutIfNeeded() in cellForRowAt with no success.

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

As requested cellForRowAt code attached.

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...

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:). For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. The table view'€™s delegate in tableView(_:cellForRowAt:) should always reset all content when reusing a cell. If the cell object does not have an associated reuse identifier, this method is not called. If you override this method, you must be sure to invoke the superclass implementation

Apple docs

hide indicator view in storyboard too

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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