Search code examples
iosobjective-cuitableviewios6autolayout

iOS 6 AutoLayout with multiple tableviews all dynamic heights


Okay so I am stuck. In my app I have a product view. On this view there are two nested table views inside a scroll view. The first tableview is only one cell and holds static text. This text length is dynamic based on which proudct you pick. The second tableview consists of multiple cells based on how many sub products are linked to that prodcuct. So in iOS 5, it was pretty easy to dynamically move the frame of each table view based on how large the text was in the first table view. I could then just sum up the height of all the table views and that would be the height of the scroll view. The issue is when I turned on auto layout things got hozed. From my understanding scrollviews have changed. We cannot use the frame or content size of a scroll view. So the first thing I did was changed all that over to contraints. I did the following below.

CGSize descriptionSize = [descriptionDetail sizeWithFont:
                         [UIFont systemFontOfSize:14]
                         constrainedToSize:CGSizeMake(320, CGFLOAT_MAX)
                         lineBreakMode:NSLineBreakByWordWrapping];

self.theScroller.translatesAutoresizingMaskIntoConstraints= NO;
[self.theScroller setContentInset:UIEdgeInsetsMake(0, 0, (descriptionSize.height+ (43 * [Solution count])), 0)];

Now this above works flawless. The scroll views scrolls great and is the correct height with auto layout on.

Now the part I am stuck on is how do I dynamically move the two tableviews up and down based on how long the description is or how many cells are in the second table view? The frames of the table view dont seem to have any effect on where they are located. I tried messing around with the contraints but didn't seem to get anywhere. Any help would be greatly appreciated. Thanks

Picture of view hierarchy

enter image description here


Solution

  • You could make life easier for yourself by doing all this using a single UITableView I think.

    Your upper UIView with the UIImageView in it is the tableHeaderView, your first 'single row UITableView' is the first section in the UITableView and your table view with dynamic content is in further sections of the UITableView.