UITableView dynamic cell heights only correct after some scrolling

后端 未结 24 1230
误落风尘
误落风尘 2020-11-29 16:41

I have a UITableView with a custom UITableViewCell defined in a storyboard using auto layout. The cell has several multiline UILabels.

24条回答
  •  一整个雨季
    2020-11-29 17:28

    Add a constraint for all content within a table view custom cell, then estimate table view row height and set row hight to automatic dimension with in a viewdid load :

        override func viewDidLoad() {
        super.viewDidLoad()
    
        tableView.estimatedRowHeight = 70
        tableView.rowHeight = UITableViewAutomaticDimension
    }
    

    To fix that initial loading issue apply layoutIfNeeded method with in a custom table view cell :

    class CustomTableViewCell: UITableViewCell {
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.layoutIfNeeded()
        // Initialization code
    }
    }
    

提交回复
热议问题