0

In storyBoard i have given top, bottom, leading and trailing constraints to tableview in a view. i have given tableview height as contentSize.height. As per tableview height, no of cells that can visible in tableview 3 or 4 or etc(depending upon content size it may vary). But the problem is table view height is increasing as per content size. if no of cells are more, then it should compare actual height of the table view with content size and if contentSize is lesser than tableview Actual height then only it should change the height of tableview.

if tableViewActualHeight > contentSize {
// then only change tableview height
    tableViewActualHeight = contentSize
}

How can we get & where to place that code?

  • your question is not clear. What you want to achieve exactly ? – Ketan Parmar Jul 04 '17 at 05:26
  • i need to restrict table view height if no of cells are more – Bhagyashree Myanamwar Jul 04 '17 at 05:29
  • what do you mean by more ? how many ? Give fixed height to table view then! – Ketan Parmar Jul 04 '17 at 05:43
  • yeah i have given fixed height. But the issue is i need to change tableview height dynamically by content size. for example : actual table view height = 200 and content size is 100 then i need to change table view height to 100 (actual height > content size), if content size is > 200 then no need to change the table height (actual height < content size) – Bhagyashree Myanamwar Jul 04 '17 at 05:57
  • do you want fixed height or dynamic ? because first you said you want to restrict the height, then you said you want dynamic height!! – Ketan Parmar Jul 04 '17 at 05:59
  • if content size > actual height --> Fixed if content size < actual height --> Dynamic – Bhagyashree Myanamwar Jul 04 '17 at 06:01
  • okay! add this sentence in your question so any one can understand!! – Ketan Parmar Jul 04 '17 at 06:02
  • please refer [https://stackoverflow.com/questions/44883797/set-tableview-height-dynamically-and-restrict-to-some-extend-swift-3/44917152#44917152](https://stackoverflow.com/questions/44883797/set-tableview-height-dynamically-and-restrict-to-some-extend-swift-3/44917152#44917152) its working fine. – Bhagyashree Myanamwar Jul 05 '17 at 04:44

2 Answers2

1

OK here is an idea for that

enter image description here

First get height of tableview by yourTableView.frame.size.height. Now calculate height of your tableView cells. Lets assume the cases.

Assuming cell is of height 50 and tableview if height 200

First case

If you have 2 cell, then total cell height is 100. Now check if 100<200 bottomConstraint of tableView will be 200 - 100, that is 100.So here tableView height becomes 100

Second Case

If you have 5 cells ,then total cell height is 250 .Now check if 250<200 bottomConstraint will be ZERO

Please let me know if its helps you or having trouble to understand

Vikky
  • 914
  • 1
  • 6
  • 16
0

You need to set one more constraint to your tableview - Fixed height.

Now take(or connect) outlet of that constraint and then set constant of that outlet as per your condition. If your content size is greater then your tableview do nothing else set your height constraint's constant as content size like,

  heightConstraint.constant = 20  // Your contentsize instead of 20

You need to do that after your tableview successfully reload, other wise it will not give correct content size.

If your cell or row's height is fixed then you can count content height your self. You can multiply row height with number of row (array count)!

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75