1

What is the best approach to hide UI elements in a UITableView with UITableViewAutomaticDimension?

I have multiple labels, buttons, imageviews in a cell. For specific condition I want to hide some of them and want the tableview to resized accordingly.

Kindly correct me if I'm missing something.

Ujesh
  • 1,698
  • 2
  • 23
  • 35
Ankit Kumar Gupta
  • 3,994
  • 4
  • 31
  • 54
  • You will have to set their height constraints to 0 in the case of `UIImageView` and just simply set the text of the `UILabel` to an empty string provided its height is inferred from its content. If you have explicitly set the the heights, you will need to take an outlet of the constraint and set it to 0. – Rikh Mar 17 '17 at 07:37
  • There are about 25 elements in the cell and some labels are of line 0 which may be present or not. So writing height constraints for them will be troublesome. Kindly suggest any other approach if possible – Ankit Kumar Gupta Mar 17 '17 at 07:42
  • As long as the `UILabel` are empty, their height will be 0 and you will not have any issues with automatic dimensions, however their vertical spacing constraints will still be active. You can programatically iterate through all the elements in a view and then iterate over each element's constraints till you find the height constraint and set it to 0, this will require a lot of processing however. Apart from this, I'm sorry, I have no idea :D – Rikh Mar 17 '17 at 07:51
  • No probs Rikh thanks for the time and effort. Will let you know if I get something useful. – Ankit Kumar Gupta Mar 17 '17 at 07:53

2 Answers2

3

Use StackView.

StackView will manage it, when elements hide they resize automatically. use StackView in tableview cell

Click Here for how to use StackView

Click Here For Example, This will help you to more understand.

Ujesh
  • 1,698
  • 2
  • 23
  • 35
0

If you are using UITableViewAutomaticDimension then you must update constraints programatically.

i.e if you have 2 labels and 2 button, and if you want to hide on label on click of one of button. then you have to manage height constraints of that label to manage automatically manage cell heigh.

Jatin Chauhan
  • 1,107
  • 1
  • 8
  • 21
  • Thanks for the help @jatin But the problem is that there are about 25 elements in the cell and some labels are of line 0 which may be present or not. So writing height constraints for them will be troublesome. Kindly suggest any other approach if possible. – Ankit Kumar Gupta Mar 17 '17 at 06:55
  • 1
    Then you have to go with stackview. but the only problem is the support of stackview is iOS 9 and latter iOS version. – Jatin Chauhan Mar 17 '17 at 06:57
  • My project supports iOS 9+ so no issues with that. Will give it a shot and let you know. – Ankit Kumar Gupta Mar 17 '17 at 06:59
  • Also i m facing an issue with the image view there. When i fetch the image using sdwebimage and scroll the imageview size increase every time a scroll any idea why? – Ankit Kumar Gupta Mar 17 '17 at 07:03