1

I'm trying to make all the rows in my table have their own row height based on the lines of text in the label in that cell. My point of departure is the Wenderlich Tutorial here: https://www.raywenderlich.com/129059/self-sizing-table-view-cells

I'm using the method UITableViewAutomaticDimension as you can see below. But for some reason when I run the simulation the rows all come out the same height. It doesn't seem to matter what number I put in for the estimated row height in the code or what number I put in for Row Height in the Table View Cell in the storyboard. It always comes out the same height for all the rows.

What am I missing here? Thanks everybody!

import UIKit

class LoLFirstTableViewController: UITableViewController {

    var tasks:[Task] = taskData

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 60.0
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tasks.count
    }

    @IBAction func cancelToLoLFirstTableViewController(_ segue:UIStoryboardSegue) {
    }

    @IBAction func saveAddTask(_ segue:UIStoryboardSegue) {
        if let AddTaskTableViewController = segue.source as? AddTaskTableViewController {

            if let task = AddTaskTableViewController.task {
            tasks.append(task)

                let indexPath = IndexPath(row: tasks.count-1, section: 0)
                tableView.insertRows(at: [indexPath], with: .automatic)
            }
        }
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
    -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath)
        as! TaskCell
        let task = tasks[indexPath.row] as Task
            cell.task = task
        return cell
        }
}

` contraints simulation

Bob F.
  • 93
  • 1
  • 11
  • show your cell, You must have wrong autolayout constraints. – Umair Afzal Jan 18 '17 at 13:15
  • Do you have content that should size differently cell-to-cell? Regarding *"It doesn't seem to matter what number I put in for the estimated row height in the code or what number I put in for Row Height in the Table View Cell in the storyboard"*; neither of those things should matter in determining the final height of an auto-sizing cell. – Connor Neville Jan 18 '17 at 13:59
  • Yes, the content should be sized differently cell-to-cell. My sample text in there ranges from a single line to 3 lines. I'm attaching screenshots of my constraints and what the simulator's output currently looks like. Thanks Umair and Connor! – Bob F. Jan 22 '17 at 20:46

1 Answers1

0

You have no constraints on the label, so there is nothing about the label that would cause anything else to change. And the reason you have no constraints on the label is that you are doing everything with stack views. But the stack views are not going to be magically sized from the inside out. Thus, they are totally getting in your way.

I would suggest eliminating the stack views entirely. Now you'll be able to do this in a normal fashion, and it will work.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • This is a downloadable project that sizes cells in accordance with the amount of text in a label. You can download and run it, and you will see that it does work. https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch08p424variableHeights/ch21p722variableHeights/RootViewController.swift – matt Jan 22 '17 at 21:17
  • Awesome, thanks a lot, matt! So then how would I add back in a check box button on the right without adding back in a stack view? – Bob F. Jan 22 '17 at 21:19
  • Just put a check box button on the right. It could be the accessory view, or you can add it as a subview of the `contentView` just like the label. The key thing is to get the constraints right. You probably want this thing vertically centered, with a height and width constraint, and positioned at the right edge of its superview. — Stack view really does seem like overkill in a table view cell anyway. – matt Jan 22 '17 at 21:21