swiftuitableview

UITableViewCell self sizing mess up


I am trying to make my UITableViewCell something like this: enter image description here

I decide to add an UIView to contain my text so that I can make the border. This is my .xib config enter image description here

My ViewController's TableView setup

        tableView.delegate = self
        tableView.dataSource = self
        self.tableView.rowHeight = UITableView.automaticDimension
        self.tableView.estimatedRowHeight = 100
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0.0
        }
        self.tableView.isScrollEnabled = false

But things end up like this

enter image description here

Is it something about my constraints setup? Or I am missing something, can someone point it out? Thanks in advance


Solution

  • You're missing a constraint between one of these two labels and the border view, which will make the height of the cell dynamic. These two labels are also center Y so you can make any of them have a bottom constraint with BorderView, i.e., I will do it with AmountTypeLabel and change the constraint bottom value to 10.

    enter image description here

    This is the output:

    enter image description here

    Just simple thinking about constraint from top to bottom with all subviews will make the cell have dynamic height:

    ContentViewTop
          |
     BorderViewTop
          |
        Label
          |
     BorderViewBottom
          |
    ContentViewBottom