2

My app displays business information in a tableView and mapView. In the tableView, I measure the size of the text that populates the business name label and adjust the cell height appropriately for longer names in a custom heightForRowAt function. It works well:

enter image description here

In my mapView, this doesn't work so well, as you can see the price and open/closed labels are being cut off below:

mapView

I replicate the same code to determine the height of the cell. In this case, the "cell" is the primary horizontal stackView, which has an embedded image stackView and info stackView (same as in the tableView). My print statements indicate the height of the horizontal stackView is increasing as required, but it's pushing the content down rather than increasing the horizontal stackView upwards. That stackView is pinned to right, left and bottom of the superview, so theoretically that should not occur.

This code sets my horizontalStackView height:

    func setStackViewHeight(heightForCellWithLocationName name: String) -> CGFloat {

    var size = CGSize()

    if let font = UIFont(name: ".SFUIText", size: 17.0) {
        let fontAttributes = [NSAttributedStringKey.font: font]
        size = (name as NSString).size(withAttributes: fontAttributes)
    }

    let normalCellHeight = CGFloat(96)
    let extraLargeCellHeight = CGFloat(normalCellHeight + 20.33)

    let textWidth = ceil(size.width)
    let labelWidth = ceil(nameLabel.frame.width)

    if textWidth > labelWidth {
        print("***********EXTRA LARGE CELL HEIGHT****************")
        return extraLargeCellHeight
    } else {
        print("***************NORMAL CELL HEIGHT*****************")
    return normalCellHeight
    }
}

And I use this code in my function that populates the stackViews with data:

                horizontalStack.frame.size.height = setStackViewHeight(heightForCellWithLocationName: location.name)

            if horizontalStack.frame.height == 116.33 {
                infoStackView.frame.size.height += 20.33
            }

What am I missing?

Pigpocket
  • 449
  • 5
  • 24
  • are you setting your label's `numberOfLines` to `0`? – mfaani Feb 21 '18 at 03:36
  • Yes @Honey, with word wrap on. – Pigpocket Feb 21 '18 at 03:38
  • I strongly advise you to see this moment of the [WWDC video](https://developer.apple.com/videos/play/wwdc2015/218/?time=215) and [this other moment](https://developer.apple.com/videos/play/wwdc2015/218/?time=1432). This way you avoid calculating the cell height. You said *That stackView is pinned to right, left and bottom*. Why not the top? – mfaani Feb 21 '18 at 03:46
  • So that it will expand up when it grows in size? – Pigpocket Feb 21 '18 at 03:53
  • it will expand up to the sum of their intrinsic sizes. For labels the intrinsic size is calculated based on the `font-family` and `font-size` – mfaani Feb 21 '18 at 04:07
  • @Honey so I need to sum up all of the heights of the labels, imageViews, etc, yes? – Pigpocket Feb 21 '18 at 04:13
  • You have to follow [here](https://stackoverflow.com/a/30933372/5175709). If you do such, then No you don't need to sum them all up. It's done for you... – mfaani Feb 21 '18 at 04:18
  • This all seems to suggest that my "cell" should be a UITableViewCell, rather than simply a horizontal stackView, is that correct? Right now, it is not configured as a UITableViewCell, but rather a UIStackView with embedded labels, imageViews, etc. The link doesn't appear to be adaptable to other types of view heights. By the way, I tried using UITableViewAutomaticDimension and .estimatedRowHeight in my TableViewController class, but had to come up with a custom method instead. – Pigpocket Feb 21 '18 at 04:48
  • not sure what you mean by that. Isn't your stackview placed inside a tableviewcell? *In this case, the "cell" is the primary horizontal stackView, which has an embedded image stackView* – mfaani Feb 22 '18 at 14:52
  • It was not. It is now. And based on my understanding the tableViewCell must also live inside a tableView itself, so I've added that as well. That's create a new set of problems, but at least I think I'll be able to implement my heightForRowAt function successfully when I figure the other stuff out. – Pigpocket Feb 23 '18 at 00:18

0 Answers0