Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView strange layout behavior changes on scroll

I have a UITableView with custom UITableViewCell designed to have automatic height.

The behavior is that in the first load of my UIViewController with UITableView displays my labels with partial texts. Then when I scroll to bottom then scroll to top, I get the desired look for height and for texts.

Here is the screenshots:

Initial look for the view controller:

enter image description here

Here is the look after scrolling bottom then top (This is the correct look for my implementation some Lorem text is removed by me):

enter image description here

Edit 1:

Code:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 101 // much heigher than most cells. But no use.

In the UI Builder, the UITableViewCell's "Row Height" set to "Default" (Custom not checked)

In the UI Builder, the UITableView's "Row Height" set to "101" (also tried to set it to "0" but same result.)

like image 742
malhobayyeb Avatar asked Mar 24 '15 11:03

malhobayyeb


1 Answers

Getting auto layout to work on a UITableViewCell is to ensure you have constraints to pin each subview on all sides — that is, each subview should have leading, top, trailing and bottom constraints.

Furthermore, you need a clear line of constraints going from the top to the bottom of the contentView. This ensures that auto layout correctly determines the height of the contentView based on its subviews.

The tricky part is that Interface Builder often won’t warn you if you’re missing some of these constraints; auto layout simply doesn’t return the correct heights when you run the project. For example, it may return 0 for the cell height, which is a clue that your constraints need more work.

If you run into issues when working with your own projects, try adjusting your constraints until the above criteria are met.

check the link for details:http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift

like image 81
Ram Vadranam Avatar answered Nov 15 '22 09:11

Ram Vadranam