2

I'm using a TableView and have a custom TableViewCell that I've added a subview to.

The problem is that I need the subview's height to change sometimes and therefore, the table's contentView would have to be updated as well as the row's height.

The subview of the custom TableViewCell is represented by the yellow background.

These images show what's currently happening in my simulator.

  1. On Load enter image description here

  2. After the event that causes the subview's height to increase enter image description here

What's the best approach to take with something like this?

Should I use constraints? And if so, what kind of constraints should I use? Would I have to then reload the tableview too every time the subview's size changes?

Here is the code I'm currently using for my custom TableViewCell:

import UIKit

class CustomCell: UITableViewCell {

    var newView: UIView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.newView = UIView(frame: self.frame)
        self.newView.backgroundColor = .yellowColor()
        self.addSubview(newView)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        self.newView.frame.size.width = self.frame.size.width // because self.frame.width is different than it was in the init method
   }

    func somethingHappenedThatMySubviewHasToIncreaseInHeight() {
        self.newView.frame.size.height = self.frame.size.height + 40
    }
}
Thomas
  • 2,356
  • 7
  • 23
  • 59

1 Answers1

5

The best approach is to use Auto Layout and self-sizing cells. Setup constraints in storyboard for your custom cell.

You will not need to reload the tableView. Each cell will automatically adjust its height, based on how much vertical space its subview takes.

For more information, see the detailed walkthrough by smileyborg in his answer to Using Auto Layout in UITableView for dynamic cell layouts & variable row heights.

Community
  • 1
  • 1
  • yeah I downloaded his custom tableviewcell project (https://github.com/smileyborg/TableViewCellWithAutoLayoutiOS8.git) and tried to modify his custom TableViewCell so it would use a UIView as a subview of the UITableViewCell (and be dynamic in height), but no luck so far :( – Thomas May 19 '15 at 19:41
  • Does your view set its `intrinsicContentSize?` It would have to tell its superView (the tableView's contentView) what its height is, so Auto Layout can properly size the contentView, which then sizes the cell height. –  May 19 '15 at 19:53
  • I have not, but I'm not sure if I should be doing that with the approach I'm taking. I've uploaded my existing code to github so you can take a look. I'm using smileyborg's project with modifications to add the dynamic UIView subview. However, when I call self.newView.frame.size.height = 400, it doesn't seem to stretch the tableview cells out. https://github.com/thomasbaldwinj/Table-View-With-Dynamic-Subview-Height/tree/master/TableViewCellWithAutoLayout – Thomas May 19 '15 at 21:56
  • 1
    Setting the intrinsic size is what Auto Layout expects. When it goes to layout the cell, it asks the subViews to layout. Each subView has to know how much room it needs. Then the cell knows its height because only one height will now satisfy the constraints. You definitely don't want to change frames when using Auto Layout. Auto Layout is a replacement for frames. –  May 19 '15 at 22:02
  • 1
    Here's a great article that explains layout, constraints, and layoutSubviews. http://www.objc.io/issue-3/advanced-auto-layout-toolbox.html –  May 19 '15 at 22:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78265/discussion-between-thomas-and-petahchristian). – Thomas May 20 '15 at 01:38