UITableViewCell 标签的内容丢失

问题描述 投票:0回答:1

我正在设置一个tableView,但是在运行应用程序时,标签的内容不稳定,有时显示完整内容,有时不显示。寻求建议。 我附上了 .xib 设置的图像以及 tableView 的显示方式。 代码是我的ViewController


    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "customCell")
        tableView.estimatedRowHeight = 44
        tableView.rowHeight = UITableView.automaticDimension
        tableView.dataSource = self
        tableView.delegate = self
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as? CustomTableViewCell else {
            return UITableViewCell()
        }
        switch indexPath.row {
        case 0:
            cell.titleLabel.text = "Remaining Last Statement Balance"
            cell.amountLabel.text = "$22198.45"
        case 1:
            cell.titleLabel.text = "Minimum Payment Due"
            cell.amountLabel.text = "$2222198.45"
        case 2:
            cell.titleLabel.text = "Current Ballance"
            cell.amountLabel.text = "$2198.45"
        case 3:
            cell.titleLabel.text = "Last Statement Balance"
            cell.amountLabel.text = "$2198.45"
        default:
            break
        }

        return cell
    }
    
}
swift uitableview nslayoutconstraint
1个回答
0
投票

您可以在返回单元格之前强制执行正确的布局

cell.layoutIfNeeded()
return cell 
© www.soinside.com 2019 - 2024. All rights reserved.