3

I have 4 components in my cell.

  1. UIView
  2. UITextView
  3. UIImageView
  4. Another UIView on top of the UIImageView
  5. UIView
  6. UIView

These content can be changed according the data that comes from the server(Sometimes it needs to hide the 3rd image view, sometimes 5th UIView). I'm using auto layout and with the auto layout what is the way to change height of the cell?

codebot
  • 2,540
  • 3
  • 38
  • 89
  • check this : http://stackoverflow.com/a/38064420/3901620...you need to set constraint as given in this example and add two line of code in viewDidLoad. – KKRocks Apr 25 '17 at 06:47

4 Answers4

3

You can achieve this easily by using UIStackView. Stack views are a powerful tool for quickly and easily designing your user interfaces. Their attributes allow a high degree of control over how they lay out their arranged views.

Here is link for tutorial - Tutorial

If you hide a view from stackview it conveniently disappears from the layout, but it's still a subview of the stack view. So you don't have to do anything special if you want to bring it back later on. And by using self sizing cells, cells will automatically expand or collapse based on stack view height.

2

Do following steps:

  1. When you hide any component give that component frame height as 0 and reload tableview. If you giving any component constant height then take outlet of height constraint and make it zero and specify its constant height again when you unhide.

  2. When you unhide give him specific frame height and reload tableView.

  3. heightForRow must returnUITableViewAutomaticDimension

As you already taken components from the storyboard so compiler understands the height of cell and work accordingly.

If you still facing issue you can ask.

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
0
this two methods are use for dynamic cell height

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
    return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    return UITableViewAutomaticDimension
}
Chetan Hedamba
  • 229
  • 2
  • 7
0

I post this for other man meet my issue.

Do not apply any change that will change the cell height in layoutSubviews, like hidden view or change any view's height.

Jadian
  • 4,144
  • 2
  • 13
  • 10