2
votes

I'm having an issue combining Autolayout with dynamic cell resizing and having cells grow when selected.

My current implementation has the cell set to height 70 in unselected state. It is laid out with AutoLayout and has extra information hidden, from pixel 71 to 220:

Normal cell label
----------------   70px
UILabel
UITextView
UIButton
----------------   220px

The expanded info in this case is positioned with AutoLayout to be below normal cell label.

The UITextView in the expanded info should grow to fit the content, increasing the size of the cell beyond 220px.

I calculate this in heightForRow, by dequeueing a cell, configuring it and asking for its height using systemLayoutSizeFittingSize:.

Unfortunately, this fails to give me the correct size about 75% of the time. The exception is when:

 1. I load the normal cell and tap to expand.
 2. I then scroll down, and scroll back up to the expanded cell.
 3. The expanded cell is now the correct height after recycling (still expanded) 

I've heard systemLayoutSizeFittingSize: needs top to bottom AutoLayout constraints, but I can't enforce a bottom constraint as this would mean these elements would be visible in the unexpanded state.

The problem appears to be stemming from AutoLayout, and the fact it doesn't produce the correct size most of the time, even with setNeedsLayout and layoutIfNeeded.

For the most part, I've been using the following tutorial: http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout

1
Thanks - I'll definitely look into PureLayout. The issue they answer however is based on UITableViewCell's with no expansion in height on selection. I've previously got this working. With expansion however, there are no AutoLayout constraints pinning subviews to the bottom of .contentView. I believe this brings issues when trying to obtain view height using AutoLayout methods.Christopher Kevin Howell

1 Answers

0
votes

Have you tired

[tableView reloadData]

or

[tableView reloadRowsAtIndexPaths:<#(NSArray *)#> withRowAnimation:<#(UITableViewRowAnimation)#>]

The cell needs to be laid out again, so when you scroll down and back up the cell gets recycled and correctly determines its height when it is laid out again.