1

I want to create a view like this. For that I took one view. Inside it, label(Specification) and then for data of specification I took one tableView. Data for specification could be 2,5,7 anything. Means I am getting array of specification, which could have any no. of elements. Now I want to set height of this view according to height of tableView and after this specification block I have another block which has y position according to specification view. How can I do this?

image

image2 This is the image of output what I am getting. I don't want this scroll in tableView, instead of that I want it show with its maximum height in view.

Riddhi Shah
  • 733
  • 4
  • 11
  • 25
  • 1
    Try setting a height constraint on the table view, and change its constant in code based on the number of cells times the cell height. – Mike Taverne Apr 02 '16 at 17:27
  • how can I change height of table and view both??. I tried but my view's height is not changed. – Riddhi Shah Apr 05 '16 at 04:42
  • 1
    I'm not sure this will solve your issue. But what you could try is creating a height constraint on your table view in your storyboard. Then, create an outlet for the constraint by selecting it and control+dragging to your view controller. Give the constraint outlet a name like tableViewHeightConstraint. In your view controller, set tableViewHeightConstraint.constant to the desired height (number of cells * cell height). – Mike Taverne Apr 05 '16 at 05:04
  • I have added my code.. I tried in your way, but I am not able to do that. and I also have question that if I want to change my view's height according to table, what constraints should I give? – Riddhi Shah Apr 05 '16 at 05:33
  • I'm sorry that didn't work. I don't know what else to suggest. – Mike Taverne Apr 05 '16 at 05:36
  • ok. Thanks for the suggestion. – Riddhi Shah Apr 05 '16 at 05:41
  • This answer is right, but I need to add only this line. self.view.layoutIfNeeded() – Riddhi Shah Apr 05 '16 at 06:07
  • Nice, glad it worked! – Mike Taverne Apr 05 '16 at 06:39

2 Answers2

0

You can set relation of the height constraint of tableView as <=. So that it will automatically adjust the height of the tableView, according to the data.

0

Change the UITableView height after loading the content in table using GCD.

Inner view of UITableView is based on ContentSize and outer view is based on frame size so just change frame size same as ContentSize. Outer frame size depend on super view frame size but inner view size is depend on content.

Follow the selected answer in Resizing UITableView to fit content

Hope it will help you.

Community
  • 1
  • 1
Sujay
  • 2,510
  • 2
  • 27
  • 47