johnjay22113 johnjay22113 - 1 year ago 63
Swift Question

Set tableview cell row height dynamically?

I have a tableview with a label and an image. in some cells, there is no image as i used

to remove it from the cell. When there is an image in the cell, the row height is 445 and "Custom" is checked off.

how can i set the row height dynamically according to how long the label is instead of how long/big the imageview is after i remove the imageview?

Rob Rob
Answer Source

If you want dynamic row height, you can define your constraints (making sure they're unambiguous), set the label's lineCount is set to zero, and then in viewDidLoad, tell it that rows should automatically adjust their height:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44

If you want to hide/show a UIImageView as well, I must confess that I'm not crazy about the removeFromSuperview approach (because when the cell is reused, you have to re-add the image view, and possibly rebuilding its constraints, too) there are a few options:

  1. You could have a different cell prototype for the cell with the image view and another without the image view. Then cellForRowAtIndexPath just needs to instantiate the right cell.

  2. You could go ahead and define a full set of constraints that are unambiguous for both the presence of the image and without the image. And then, you can activate the constraint and set the image view to hidden as appropriate:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell
        let image = ...  // let's say it was an optional, set if needed, left `nil` if not
        cell.customImageView?.image = image
        if image == nil {
            cell.customImageView.hidden = true
   = false
            cell.customLabel.text = ...
        } else {
            cell.customImageView.hidden = false
   = true
        return cell

    The trick when having competing sets of constraints that dictate the height of the cell is to just make sure that they have different priorities and/or they use inequality so if both sets are in effect, you don't have an unsatisfiable conflict (e.g. the image view might have higher priority).

  3. You can, if you want, go "old school" and programmatically determine the size of the label field, and then implement heightForRowAtIndexPath, but auto layout makes this process unnecessary.