1

I am trying to resize a tableviewcell based on the size of a UILabel that can be one or more lines. I need autolayout turned ON for the rest of the app, so I don't want to turn it off to get this working. Target is iOS 7 and iOS 8.

I have tried several solutions, but for some reason I can't seem to get the correct height of the label to automatically adjust or to adjust the cell height.

The problem is occurring in this method: calculateHeightForConfiguredSizingCell. I have put a sample project on Git, so you can see what I am seeing.

https://github.com/mdaymond/cellResizer

This example is based on this article: http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout

Update I checked in an update to the code. It's ALMOST working the way I want and calculating programatically, but for some reason the label height isn't quite sized correctly - it's not getting the full height required. Problem with the original code was that the label needed an explicit width.

mday
  • 427
  • 1
  • 7
  • 22
  • What exactly is you problem? You don't need to turn off Auto Layout. You can just decide to not use it in one place. You can even use manual layout and Auto Layout in the same view. – dasdom May 05 '15 at 12:29
  • are you targeting anything < than iOS 8.0? because if not you can simply setup constraints in storyboard and do not have to to calculate anything manually! – André Slotta May 05 '15 at 12:32
  • @dasdom - The problem is that the label is not resizing nor is the cell height. Target build is iOS 7 and 8 – mday May 05 '15 at 16:39
  • @AndreSlotta - Yea. I wish. iOS 7 and iOS 8 - clarified my question as well.. thank you. – mday May 05 '15 at 16:40

3 Answers3

1

Here we go, if you are supporting IOS 7 then you need to implement UITableViewDelegate protocol in your class and then override:

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

}

for more information about Dynamic custom UITableViewCell's height based on label text length (check this out) and you can place the code in the heightForRowAtIndexPath function: height based on label text length

Also override :

 func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
       // example to return the estimated height 
        return 280
    }

Note : by supporting IOS 7 you should handle it manually. there is no such easier way as IOS 8.

But if you are only supporting IOS 8 and later then you can do it simply in the following two lines :

self.tableView.estimatedRowHeight = 280
self.tableView.rowHeight = UITableViewAutomaticDimension
Community
  • 1
  • 1
AaoIi
  • 8,288
  • 6
  • 45
  • 87
  • In the sample code I provided you can probably see that I have both of these methods implemented and the delegate. Going to try the link you referenced now to see if that leads my anywhere. – mday May 05 '15 at 15:27
  • Sorry, but this doesn't fix the actual problem The linked article links the the same article I reference above. Thanks for trying! – mday May 05 '15 at 16:38
  • @mday , Well as you know I've been trying for the past months to fix cell based on the UILabel height. by choosing to fix it programmatically you should do as i said and thats enough and it works great with me. remember that IOS7 means a pain to handle such a thing. – AaoIi May 06 '15 at 04:30
0

Actually i have download your source code and doing following changes. and UILabel is now support multiline.

Step 1 : Removed programmatically calculate row height. that means commented heightForRowAtIndexPath in your demo.

Step 2: Set following layout constraints on UILabel in your UITableViewCell.

enter image description here

Step 3 : Set number of line to 0 in your xib.

Output :

enter image description here

Jatin Patel - JP
  • 3,725
  • 2
  • 21
  • 43
  • This will probably work. Trying it now to confirm. However, I need to programatically calculate the height as my cells become more complex down the road.. – mday May 05 '15 at 15:25
  • I checked in an update to the code. It's ALMOST working the way I want and calculating programatically, but for some reason the cell heights aren't quite sized correctly. Problem with the original code was that the label needed an explicit width. – mday May 05 '15 at 17:25
  • @mday even your cell is too complex even though you can manage with auto layout without programming. – Jatin Patel - JP May 05 '15 at 17:26
  • 1
    In `calculateHeightForConfiguredSizingCell`, you might want to call `preferredMaxLayoutWidth` and then `setNeedsDisplay` on your label before calculating the cell size. – lostInTransit Nov 03 '15 at 06:28
0

Here is my code.

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
     self.scoutPropertyTable.rowHeight = UITableViewAutomaticDimension + 35
    return self.scoutPropertyTable.rowHeight
}
clearlight
  • 12,255
  • 11
  • 57
  • 75
Alvin George
  • 14,148
  • 92
  • 64