ChatGPT解决这个技术问题 Extra ChatGPT

Autolayout is ignored in Custom UITableViewCell

Despite having set constraints to all elements, including the vertical ones needed for the cell to calculate its height, auto-layout seems to be ignored: all cells are squeezed.

Here's a screenshot of the result and of the constraints in the storyboard:

https://i.stack.imgur.com/AMMBb.png

https://i.stack.imgur.com/swgxR.png

In the VC that holds the tableView, here's the code in viewDidLoad:

tableView.estimatedRowHeight = 120.0
tableView.rowHeight = UITableViewAutomaticDimension

Commenting out the second line gives cells with a height of 120.0 but Autolayout is ignored as well.

Update

To simplify the interface, I've left a single label with, as constraints:

Leading space to superview

Top space to superview

Fixed width and height (100 & 100)

Bottom space to container margin to make sure that the cell has all vertical constraints to determine its height

And with this simplified interface, auto-layout is still not taken into account, which hints me that the problem did not come from badly set constraints.

In the Size Inspector, the row height is set on 120 and Custom is checked. The cell has the right custom class, the cell reuse identifier is correct.

Check your debug terminal - are there any NSLayoutConstraint logs in there suggesting something isn't working as expected?
Nothing appears in the debug terminal @MatthewHallatt, thank you.
@Kqtr try commenting these two lines and then run it normally once to check is there any change?
@TusharSharma Commenting out the two lines has the same effect as commenting out the rowHeight line only: the cell has a height of 120.0, but auto-layout doesn't work.
Does interface builder show any constraints as missing or ambiguous? Check for the little yellow or red exclamation mark.

M
MoVod

Auto-layout was ignored because both the prototype cell AND the UIView of the cell had been given the custom cell class in IB.

Setting the UIView back to UIView class solved the problem.

https://i.stack.imgur.com/91V45.png


I'm running into this issue even with no custom class for the Content View has anyone else encountered this?
@Destiny Faith Did you resolve the issue?
@Arjun in the UITableViewCell's attributes pane, check the Style dropdown. If it's not on Custom, it'll force you to stick with the ui elements that go with the selected built-in style. That's most likely your issue
You are my hero.
@alstr you probably just have to be more careful with the selected views. When clicking stuff on a storyboard it's easy to think you have something selected when it is in fact something else. It's harder to see when the views overlap as in the container and the content view.
J
Jaafar Barek

I think the problem is that you are implementing :

func tableView(_ tableView: UITableView, heightForRowAt indexPath: 
IndexPath) -> CGFloat {

 return x
}

You need to remove this function inorder for the tableview to calculate its automatic height for cells.


Thanks, I unfortunately do not override it: I actually do not even have a delegate for this tableView, only a data source.
Did you set the textview constraints to all the for edges of the cell? And did you disable the textView's scrolling?
The multi-line label (0 lines) has constraints for 1) vertical spacing with the number on its left 2) top space to superview 3) trailing space to superview and 4) bottom space with the Asked By label.