0

I have a tableView with static cells inside. One cell contains a map, UITextView and other controls:

enter image description here

This textview has the following constraints:

enter image description here

So far I set up the heightForRowAtIndexPath to the static value for this cell:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
    if indexPath.row == 0 {
        return 311;
    }
}

but then when the text is longer, the textview - instead of growing - contains the text inside and user has to scroll it. Basically it is always 311 in height. I want to change it, so that the whole cell adjust to the textView content.

I tried changing return 311 to return UITableViewAutomaticDimension, but then, when there's no text, the cell shrinks and the controls on the right are not visible.

So my question is - how can I set dynamic height for the cell based on the text inside, but also keep the minimum height of 311?

user3766930
  • 5,629
  • 10
  • 51
  • 104

3 Answers3

1

Add a height constraint to the text view >= the minimum value (what it would be when the cell is 311, then set the cell height to automatic.

Jerry
  • 4,382
  • 2
  • 35
  • 29
  • In my case the minimum value is 95, I added `Height >= 95` constraint, but then when the text is huge - this constraint basically stays at 95 and the textview inside is scrollable. What is weird for me is the fact that in `cellForRowAtIndexPath` I disabled the scroll by `cell.textView.scrollEnabled = false`, yet it is still scrollable – user3766930 Oct 16 '16 at 15:48
  • Is it true that you're only displaying the text? If so change to a label and use the height constraint. – Jerry Oct 16 '16 at 15:53
  • will the label support multi lines here? – user3766930 Oct 16 '16 at 15:58
  • Yes, you can set the `numberOfLines` property (it's just called `Lines` in interface builder) to 0 and it will expand to as many lines as necessary. – Jerry Oct 16 '16 at 16:06
  • 1
    You should be able to do this with a text view too. I just wrote a demo project that has a text view with "Scrolling Enabled" unchecked, a >= explicit height constraint, and automatic cell sizing and it worked. – Jerry Oct 16 '16 at 16:21
  • I tried it and it works! Sad it doesn't increase textView size when typing also! – Rikh Oct 16 '16 at 16:36
  • Of course it will not increase the size @Rikh , he needs to disable userInteractionDisbaled = false . it totally depends on requirements . – Sahil Oct 16 '16 at 16:49
  • @Sahil No i mean, i was keeping my fingers crossed automatic resize of textView worked when typing also by simply disabling scrolling, would have saved quite a lot of time! – Rikh Oct 16 '16 at 17:02
1

If you are simply displaying the text and not editing it in any way. You should try using a UILabel and the UITableViewAutomaticDeminsions will work as expected with that provided the constraints are correctly added.

What you can do is add a height constraint to the textView and then increase its size based on the size of the string you obtain. Of course you will need to call layoutIfNeeded too! UITableViewAutomaticDimensions should take care of the rest.

Rikh
  • 4,078
  • 3
  • 15
  • 35
  • You don't need to manually change the height of the constraint. A label automatically has an intrinsic size constraint at a lower priority (by default) that will set it to as high as needs to be to display all the text. – Jerry Oct 16 '16 at 16:08
  • I meant if he wants to use the textView! In that case he would have to change the height of the constraint. A UILabel will resize on its own. – Rikh Oct 16 '16 at 16:11
  • Guys thanks for all your input, I struggled a lot with trying with TextView, but then I've decided to try UILabel and surprisingly - it worked. Then I also used a hint from here http://stackoverflow.com/questions/1054558/vertically-align-text-to-top-within-a-uilabel and now my UILabel looks like UITextView and expands table accordignly. – user3766930 Oct 16 '16 at 16:28
0

I don't fully understand your question, but based on what you said maybe this works:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if UITableViewAutomaticDimension > 311 {
            return UITableViewAutomaticDimension
    }
    else {
       return 311
   }
}

I just check if the cell should be bigger than the minimum of 311, and then return either the minimum or something bigger

Eric
  • 1,210
  • 8
  • 25
  • I tried it, but it didn't work... I added `print(UITableViewAutomaticDimension)' just before the if statement and I'm getting `-1` there, I wonder why :| – user3766930 Oct 16 '16 at 15:23
  • That's strange, but in your question you said it worked, at least when there was text? – Eric Oct 16 '16 at 15:24
  • Maybe also adding @Sahil's answer helps, as he explained underneath his question – Eric Oct 16 '16 at 15:26
  • hm @Eric, sorry for not describing my problem well enough... I tried returning only `UITableViewAutomaticDimension`, but then the text is not visible at all,the cell ends on the end of the map – user3766930 Oct 16 '16 at 15:28
  • Does replacing the `UITableViewAutomaticDimension` with `textView.frame.height` twice in my question work? – Eric Oct 16 '16 at 15:32
  • hm but the textView is on the specific cell, so can I access it from heightForRowAtIndexPath? – user3766930 Oct 16 '16 at 15:33