-1

There have been so many answers to this question on stack overflow, please know that I have looked but all the solutions do not work for me. I have two UILabels in the cell, one has a definite width and height, the other which will be dynamic, I put the width about <= 250 and the height >=50. I have also set all four margins to the content view and then I tried these two methods below to get the height of the string:

var context: NSStringDrawingContext = NSStringDrawingContext()
        context.minimumScaleFactor = 0.8
        cell?.body.text = msg.body
        var width: CGFloat = (cell?.body.frame.size.width)!
        width = ((UIScreen.main.bounds.width)/320)*width
        let size: CGSize = (cell?.body.text!.boundingRect(with: CGSize(width: width, height: 2000), options:NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName: cell?.body.font], context: context).size)!
        print("size.height is \(size.height)")
        return size.height

and this method:

var optimalHeight : CGFloat
{
    get
    {
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: CGFloat.greatestFiniteMagnitude))
        //let label = UILabel(frame: CGRectMake(0, 0, self.frame.width, CGFloat.greatestFiniteMagnitude))
        label.numberOfLines = 0
        label.lineBreakMode = self.lineBreakMode
        label.font = self.font
        label.text = self.text

        label.sizeToFit()

        return label.frame.height
    }
}

and for these two, the image attached is what shows as the UILabel, which, as anyone can see, is way off.enter image description here

I have also tried implementing UITableViewAutomaticDimension in the estimatedHeightForRowAt and heightForRowAt methods, but this did not work, after two lines, it would not expand further. I tried calling estimatedRowHeight and rowHeight in the viewDidLoad() method like this:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 300 //even went as far as using 800

same thing, stopped expanding after 2 lines. I also saw something about implementing these methods:

tableView.setNeedsLayout()
tableView.layoutIfNeeded()

and yet nothing! I have looked through the raywenderlich tutorial and a lot more. I have no clue what I am doing wrong. This might be a duplicate question but I assure you I have tried every solution and nothing works for me. Please could someone take a look at this and see what I am doing wrong?

these are the screenshots of the constraints for both labels enter image description hereenter image description here

Aria
  • 389
  • 3
  • 7
  • 25
  • 2
    Why aren't you using autolayout? That's the "correctly" part that you're omitting, right there. This feature (the cell sizes itself to its contents automatically) is _all about autolayout_. – matt Oct 16 '17 at 22:33
  • @matt how do you mean? I recently started learning about swift so please could you explain further? This [article] (https://www.appcoda.com/auto-layout-guide/) just explains auto layout as having constraints, which I already have in place? or is there more to it? – Aria Oct 16 '17 at 22:38
  • For automatic row height to work, there must be a path from the top of the cell to the bottom via views and constraints. Is the label the only thing in the cell? Is it constrained on both the top and bottom? – Connor Oct 16 '17 at 23:25
  • @Connor there are two labels in the cell, one has a fixed width and height and has a constraint to be below the label in question. So the label in question is constraint on the top, trailing and leading to the content view, but the label below it is constrained to the bottom of the content view. could you please explain or point me in the right direction about the path? I'm really stuck – Aria Oct 16 '17 at 23:29
  • The top label needs a constraint to the top and a constraint to the label below. The bottom label needs a constraint to the label above and a constraint to the bottom. It sounds like you have both those, but can you confirm that? – Connor Oct 16 '17 at 23:35
  • @Connor yes, I have all those constraints, the top label to the top of the superview, the bottom of the top label to the top of the bottom label, the top of the bottom label to the bottom of the top label, and the bottom of the bottom label to the bottom of the superview – Aria Oct 16 '17 at 23:39
  • You say "yes, I have all those constraints" but you are not using them. Labels are _self-sizing_ under autolayout. If you would just stand out of the way and let the label size itself, it would do so, and if your constraints are right, so will the cell. You should not be calculating any text sizes! – matt Oct 17 '17 at 00:57
  • @matt the longer text just shows up in two lines and then cuts off, instead of word wrapping – Aria Oct 17 '17 at 01:01
  • @matt I have edited the questions and added screenshots to show the constraints – Aria Oct 17 '17 at 03:31
  • @Connor ^^ please see added screenshots – Aria Oct 17 '17 at 03:31
  • Your leading space constraints are inequalities. That's wrong. – matt Oct 17 '17 at 03:42
  • @matt I have changed them to equals, but all that did was to make it wider not longer :/ – Aria Oct 17 '17 at 11:17

1 Answers1

0

So I wrapped my dequeuing in cellForRowAt on a background thread and this prevented my cells from being dynamic. After I took that statement out, it worked perfectly! Such a waste of code time fixing something that was never broken

Aria
  • 389
  • 3
  • 7
  • 25