-1

I have added 2 labels to my cell and setup these constraints with snapkit, issue is I cant get the cell to expand correctly, it stays at its default height:

titleLabel.snp.makeConstraints { (make) -> Void in
        make.top.equalTo(contentView.snp.top)
        make.bottom.equalTo(descriptionLabel.snp.top)
        make.left.equalTo(contentView.snp.left)
        make.right.equalTo(contentView.snp.right)
    }
    descriptionLabel.snp.makeConstraints { (make) -> Void in
        make.top.equalTo(titleLabel.snp.bottom)
        make.bottom.equalTo(contentView.snp.bottom)
        make.left.equalTo(contentView.snp.left)
        make.right.equalTo(contentView.snp.right)
    }

I mapped the four edges as you can see, however I know height isnt implied by these, how can I apply a height when the content is by nature, dynamic, and could be various heights...

setup for the labels looks like this:

 lazy var titleLabel: UILabel = {
    let titleLabel = UILabel()
    titleLabel.textColor = .green
    titleLabel.textAlignment = .center
    contentView.addSubview(titleLabel)
    return titleLabel
}()

lazy var descriptionLabel: UILabel = {
    let descriptionLabel = UILabel()
    descriptionLabel.textColor = .dark
    descriptionLabel.textAlignment = .center
    descriptionLabel.numberOfLines = 0
    contentView.addSubview(descriptionLabel)
    return descriptionLabel
}()
JJPark
  • 11
  • 2

2 Answers2

1

Give the table view an estimatedRowHeight, and set its rowHeight to UITableViewAutomaticDimension. Now the cells will be self-sizing. Well, if a label is pinned by all four sides to the content view, and if the cell is self-sizing, then that's all you have to do: the label will automatically change its height to accommodate its text, and the cell will automatically change size to accommodate the label.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Downloadable example project here: https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/bk2ch08p424variableHeights – matt Feb 16 '18 at 22:42
  • yeah so what ive done is correct then? but it doesnt work! the cell retains its size – JJPark Feb 16 '18 at 22:43
  • Hey, I showed you _my_ code, and it _does_ work. Download the project and see! You have not shown your code (I don't know how the table view is configured) so who knows what you're doing wrong. – matt Feb 16 '18 at 22:46
0

First of all I think that you should add subviews to contentView in subclassed UITableViewCell class initializer method.

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
           super.init(style: style, reuseIdentifier: reuseIdentifier)
           self.contentView.addSubview(titleLabel)
           self.contentView.addSubview(descriptionLabel)
}

Secondly, make sure that in your viewDidLoad method (probably in your ViewController) these two lines are added:

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableView.automaticDimension

Of course, you should change estimatedRowHeight to accommodate your needs.

One more thing worth mentioning - you can create these constraints easier (using power of SnapKit):

titleLabel.snp.makeConstraints { (make) -> Void in
    make.top.left.right.equalTo(contentView)
}
descriptionLabel.snp.makeConstraints { (make) -> Void in
    make.top.equalTo(titleLabel.snp.bottom)
    make.bottom.left.right.equalTo(contentView)
}
3Qax
  • 324
  • 1
  • 2
  • 12