UILabel incorrectly sized in UITableViewCell (Animation after assigning text)

后端 未结 1 626
南旧
南旧 2021-01-29 02:43

The text in a UILabel is flickering after being displayed, first appearing with ellipsis on a single line, then occupying the 2 lines it fits in. Notice that the ce

相关标签:
1条回答
  • 2021-01-29 03:16

    Assuming you are changing the test in cellForRowAtIndexPath, and further assuming this does not happen with all strings, only some with certain length, then this is an iOS bug.

    See lengthy discussion on this Stack Overflow UITableViewCell post, and a possible workaround:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        tableView.setNeedsLayout()
        tableView.layoutIfNeeded()
        tableView.reloadData()
    }
    

    Notes

    1. I have also noticed that using a small value for estimatedRowHeight such as 20, which is not as tall as the smallest cell, in combination with the doubled reloadData() was also beneficial.

    2. You will need to invoke an extra reloadData() prior setNeedsLayout() when changing the cell width, such as toggling tableView.editing

    3. A robust alternative to tinkering with a label is to use a non Editable, non Selectable, none Capitalization, noScrolling enabled, no Show Horizontal & Vertical Indicator, no Bounce UITextView.


    Exercise this bug, and try out the various workarounds:

    ► Find this solution on GitHub and additional details on Swift Recipes.

    0 讨论(0)
提交回复
热议问题