1

Background

This is in reference to this post (TLDR; UITableViewCell heights should be calculated automatically if you are using Auto Layout properly).

Problem

If I add the views directly to contentView, I keep on getting this error:

018-11-25 02:17:33.514881+0200 [78571:855018] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x2825f0be0 V:|-(76)-[.TTRLabel:0x10e7c7e40'DRIVER']   (active, names: '|':UITableViewCellContentView:0x10e7cb700 )>",
    "<NSLayoutConstraint:0x2825f0b90 V:[.TTRLabel:0x10e7c7e40'DRIVER']-(10)-[.TTRLabel:0x10e7c8420'TIME']   (active)>",
    "<NSLayoutConstraint:0x2825f1c70 V:[.TTRLabel:0x10e7c8420'TIME']-(10)-[.TTRLabel:0x10e7c8a00'ADDRESS']   (active)>",
    "<NSLayoutConstraint:0x2825f1ea0 .TTRLabel:0x10e7c96a0' new office '.bottom == UITableViewCellContentView:0x10e7cb700.bottom - 26   (active)>",
    "<NSLayoutConstraint:0x2825f1f40 .TTRLabel:0x10e7c96a0' new office '.lastBaseline == .TTRLabel:0x10e7c8a00'ADDRESS'.lastBaseline   (active)>",
    "<NSLayoutConstraint:0x2825f34d0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x10e7cb700.height == 44   (active)>"
)

My problem is with this part

<NSLayoutConstraint:0x2825f34d0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x10e7cb700.height == 44   (active)>"

It's insisting that the tableViewCell height is just the standard 44 (and thus is ignoring all my autolayout work!

The only thing that solves the problem is running this

self.contentView.autoPinEdgesToSuperviewEdges()

which is weird, b/c it's not documented anywhere and doesn't make sense in the grand scheme of things.

Here is the entire UITableViewCell code for reference.

abbood
  • 23,101
  • 16
  • 132
  • 246
  • Does the cell layout correctly at the end? My guess is that it's just a problem in the initial layout when cell is laid out with "default" frame. This case I usually solve by lowering some constraint priorities to 999 instead of `.required` – olejnjak Nov 25 '18 at 01:08
  • yes the cell does layout correctly at the end, but only when i use `autoPinEdgesToSuperviewEdges` though – abbood Nov 25 '18 at 01:11
  • Do you set the `estimatedRowHeight` on the tableView? – olejnjak Nov 25 '18 at 01:39
  • yes that didn't make a difference – abbood Nov 25 '18 at 01:44
  • Could you please share the constraint that autolayout tries to break? It should be printed in the log. – olejnjak Nov 25 '18 at 01:53

2 Answers2

0

After having used this line

self.contentView.autoPinEdgesToSuperviewEdges()

for a while, we realized that scrolling tables with many cells was jittery and made the phone CPU reach 100%.

So we went back and removed the above lines, fixed the autolayout issues and then things worked well again.

So the problem was simple auto layout problems, the cell height thing was a red herring.

TL;DR: UITableViewCell heights is calculated automatically if you are using Auto Layout properly.

halfer
  • 19,824
  • 17
  • 99
  • 186
abbood
  • 23,101
  • 16
  • 132
  • 246
0
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {

}

// I add this code, it works for me, my question is on iOS 10

marvin_yorke
  • 3,469
  • 4
  • 25
  • 35
Jackie
  • 13
  • 2
  • When possible, please make an effort to provide additional explanation instead of just code. Such answers tend to be more useful as they help members of the community and especially new developers better understand the reasoning of the solution, and can help prevent the need to address follow-up questions. – Rajan May 12 '20 at 10:49