Xcode 9 Storyboard自定义TableViewCell约束配置问题求助
It sounds like the core issue here is that your number label doesn’t have constraints defining its position relative to the cell’s content view—and without that, UITableViewAutomaticDimension can’t calculate the correct row height. Let’s walk through the steps to fix this:
1. Add Complete Constraints for the Number Label
For automatic row height to work, every subview in your cell must be constrained such that the content view can determine its intrinsic height. That means your number label needs constraints that pin it to the top, bottom, leading, and trailing edges (or relative to other views in the cell, as long as those views are themselves pinned to the content view’s edges).
If you’re using Interface Builder:
- Select your number label, then use the "Pin" tool (the square with arrows at the bottom right of the IB window) to add:
- A top constraint to the view above it (e.g., a title label) with a constant like 8 points
- Leading and trailing constraints to the content view’s edges (with constants like 16 points for padding)
- A bottom constraint to the content view’s bottom edge (with a constant like -8 points)
- Make sure there are no red/yellow constraint warnings in IB—these indicate missing or conflicting constraints that will break auto layout.
If you’re adding constraints programmatically:
In your SummaryCell class’s init or awakeFromNib method, add these constraints (replace somePreviousView with the actual view above your number label):
numberLabel.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ numberLabel.topAnchor.constraint(equalTo: somePreviousView.bottomAnchor, constant: 8), numberLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16), numberLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16), numberLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8) ])
2. Finalize Your TableView Configuration
Your existing code sets tableView.rowHeight = UITableViewAutomaticDimension, but you need to complete the estimatedRowHeight setup. Set it to a value close to your expected cell height (this helps the table view calculate initial scroll positions smoothly):
override func viewDidLoad() { super.viewDidLoad() let cellNib = UINib(nibName: "SummaryCell", bundle: nil) tableView.register(cellNib, forCellReuseIdentifier: "SummaryCell") tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 80 // Adjust this to match your cell's typical height }
3. Key Checks to Avoid Common Pitfalls
- Always constrain to the content view: Never pin subviews directly to the cell itself—use the cell’s
contentViewas the parent for all constraints. - Resolve constraint conflicts: If you see auto layout errors in the console, use Xcode’s Debug View Hierarchy (Debug > View Debugging > Capture View Hierarchy) to visualize where constraints are breaking.
- Ensure labels have line break settings: If your number label (or other text views) can wrap lines, set their
numberOfLinesto 0 so auto layout accounts for multi-line text.
Once you add those missing constraints for the number label, your table view should automatically adjust row heights based on the cell’s content.
内容的提问来源于stack exchange,提问作者William Chiang

