title: UIKit: Constraints on a UITableViewCell don't work properly for the first few cells in a table view
body: <p>I have the following <code>UITableViewCell</code> subclass:</p>
  <pre class="lang-swift prettyprint-override"><code>import Foundation
  import UIKit
  
  class FlagListTableViewCell: UITableViewCell, ReusableView {
      private let flagImageView = UIImageView()
      private var cellHeightConstraint: NSLayoutConstraint!
      
      override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
          super.init(style: style, reuseIdentifier: reuseIdentifier)
          
          self.cellHeightConstraint = self.flagImageView.heightAnchor.constraint(equalToConstant: 0)
          self.cellHeightConstraint.isActive = true
                  
          self.flagImageView.contentMode = .scaleAspectFit
          self.flagImageView.translatesAutoresizingMaskIntoConstraints = false
  
          self.contentView.addSubview(self.flagImageView)
          
          // Constraints the top, leading, trailing, and bottom anchors of `flsgImageView` to `self.contentView`
          self.flagImageView.pinEdges(to: self.contentView)
      }
      
      @available(*, unavailable)
      required init?(coder _: NSCoder) {
          fatalError(&quot;init(coder:) has not been implemented&quot;)
      }
      
      func bind(to country: Country) {
          let image = UIImage(named: country.flagImageName)!
          self.flagImageView.image = image
          
          let imageViewWidth = self.contentView.frame.width
          let imageWidth = CGFloat(self.flagImageView.image!.cgImage!.width)
          let imageHeight = CGFloat(self.flagImageView.image!.cgImage!.height)
          
          let scaledHeight = imageHeight * (imageViewWidth / imageWidth)
          self.cellHeightConstraint.constant = scaledHeight
      }
  }
  
  </code></pre>
  <p>I have a <code>UITableViewController</code> subclass which displays ~250 of these cells (as reusable cells).</p>
  <p>When I display the table view however, the first three cells (or however many the device screen size fits) have incorrect constraints for some reason and cause the cells to look incorrect. However, for all other cells, they look fine. Additionally, when I scroll down and then back up to the first three cells, they look fine as well.</p>
  <p>Anyone know what could be causing this and how to fix it?</p>
  <p>I have tried the following solutions, and none have worked:</p>
  <ul>
  <li>Implementing the <code>heightForRowAt</code> delegate method and returning <code>UITableView.automaticDimension</code>, and also implementing the <code>estimatedHeightForRowAt</code> delegate and returning various values for it</li>
  <li>Implementing <code>layoutSubviews</code> method for the table view cell class to update the constraint, and then calling <code>setNeedsLayout</code> and <code>layoutIfNeeded</code>.</li>
  </ul>
  <p>Here is a screenshot of the cells with the incorrect constraints:</p>
  <p><a href="https://i.stack.imgur.com/nHeTz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nHeTz.png" alt="broken cells" /></a></p>
  <p>And here is a screenshot of the cells working as intended:</p>
  <p><a href="https://i.stack.imgur.com/F16x1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/F16x1.png" alt="correct cells" /></a></p>
  <p><strong>Edit</strong>:
  I call <code>bind(to:)</code> in the table view's diffable data source's cell provider:</p>
  <pre class="lang-swift prettyprint-override"><code>    private lazy var dataSource: FlagListDataSource = {
          let source = FlagListDataSource(tableView: self.tableView, cellProvider: self.cellProvider)
          source.viewModel = self.viewModel
          return source
      }()
  
      private func cellProvider(tableView: UITableView, indexPath: IndexPath, countryId: Country.ID) -&gt; UITableViewCell {
          let cell = tableView.dequeueReusableCell(FlagListTableViewCell.self, for: indexPath)
          let country = self.viewModel.country(id: countryId)
          
          cell.bind(to: country)
          
          return cell
      }
  </code></pre>