I want to use auto-layout for UITableViewCells. These table cells have a dynamic height (depending on text length).
I'm using [UIView -systemLayoutSizeFittingSize:]
to calculate the appropriate cell height (to return in [UITableView -heightForRowAtIndexPath:]
) but I keep getting the following results:
If I pass
UILayoutFittingCompressedSize
, I get back a CGSize of (0,0).If I pass
UILayoutFittingExpandedSize
, my app crashes with this error:*** Assertion failure in -[NSISLinearExpression incrementConstant:], /SourceCache/Foundation_Sim/Foundation-1043.1/Layout.subproj/IncrementalSimplex/NSISLinearExpression.m:620
(My guess is that this means some number is infinite.)
My implementation is simple. I calculate the height for each object, and then cache it:
MessageCell *cell = // allocate a new cell...
// set up the cell (assign text, images, etc)
CGSize size = [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
self.cellHeight = size.height; // size always equals (0, 0)
I hypothesize that this is a problem with the constraints I set, but:
- If I manually set
cellHeight
to a large value, the cells all look fine except the height is wrong. - Interface Builder gives me no warnings about ambiguous restraints
[cell hasAmbiguousLayout]
returnsNO
.- My cell has, among other things, an image set at 48x48, so a size of (0, 0) shouldn't satisfy all the constraints.
Any ideas?