4

I am trying to make a UILabel within a static UITableViewCell multiline depending on its content, which changes.

In the viewDidLoad method, I have the code that initialises the text within the label, shortly followed by:

cellLabel.text = "Here is some text, but because of how long it is, it has to span a number of lines."  
cellLabel.numberOfLines = 0  
cellLabel.sizeToFit()  
tableViewCell.textLabel?.sizeToFit()  
tableViewCell.textLabel?.numberOfLines = 0  
tableViewCell.textLabel?.lineBreakMode = .ByWordWrapping

And then I am attempting to calculate the height that the row must be in order to accomodate the multiline label:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {  
        if (indexPath.section == 2 && indexPath.row == 0) {  
            return cellLabel.frame.height  
        }  
        else {  
            return 44  
        }  
}

However, what is returned is a multiline UITableViewCell but the ending of the text is cut off and isn't displayed. I would add a value to the end of return definitionLabel.frame.height, however the content of the label varies.

Could someone please let me know how I simply have a multiline UILabel within a static UITableViewCell using Swift 2.0 and iOS 9.

Note: I have added:

tableView.rowHeight = UITableViewAutomaticDimension

And I am using a basic static table view cell which has applied constraints by default.

Daniel Bramhall
  • 1,451
  • 4
  • 18
  • 24
  • apply same as Dynamic Height of cell : tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 160.0 – Kirit Modi Sep 14 '15 at 06:07
  • sell without handling of Height of Cell : http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift – Kirit Modi Sep 14 '15 at 06:07
  • I have added tableView.rowHeight = UITableVieeAutomaticDimension and no luck. I have a static table view and so are simply using a Basic cell which has its own constraints. – Daniel Bramhall Sep 14 '15 at 06:34
  • you are run demo of http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift – Kirit Modi Sep 14 '15 at 06:35
  • Did you have a solution? I'm also having the same challenge. I have a Label inside a static table cell and I want it to be multiple line and automatic height. I have tried both `tableView.rowHeight = UITableViewAutomaticDimension` and the `override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat`. But just like yours the text is truncating. – Sayanee Apr 14 '16 at 02:13
  • Have you tried..? self.tableView.rowHeight = UITableViewAutomaticDimension; self.tableView.estimatedRowHeight = 44.0; – Nilesh Dec 09 '17 at 07:22

2 Answers2

8

On the storyboard select the cell and set the custom row height to 0. This is working for me as of iOS 9.

https://i.stack.imgur.com/WmjPX.png

This is a bug in Xcode 7 where it is setting the height of the cell in the xib xml no matter what, this was also an issue in the previous Xcode but not as bad. This refreshing occurs when the storyboard you are working on is in the foreground.

In the previous versions of Xcode i had to delete the tableviewcell rect property inside the xib file editing manually and all would be fine but the latest version keeps screwing this up.

<tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" rowHeight="0.0" id="uWP-2k-uRh">
    <rect key="frame" x="0.0" y="64" width="600" height="XX.X"/> <-- Xcode 7 keeps adding this line here

Then all you need in your viewDidLoad is

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

Thanks Apple...

  • I think you are doing something wrong. Works out of the box for me. – Webdevotion Nov 02 '15 at 15:06
  • This worked for me. I'd tried everything else including deleting the "rect" property inside the storyboard as suggested here: http://stackoverflow.com/questions/31979932/ios8-self-sizing-static-tableview-cells-with-interface-builder My project is Objective-C rather than Swift, but setting the custom row height to 0 worked perfectly. – siburb Nov 08 '15 at 04:02
  • As of Xcode 9, unchecking "custom" (instead of setting height to 0) also works. However, this resets the cell height to 44 on Interface Builder (still, better than zero). – Nicolas Miari Jan 16 '18 at 05:18
1

I've tried all the solutions available, from overriding drawRect to calling layoutSubviews but I thought I'd just chip in and say perhaps try redoing that table view cell again.

I just did and I'm baffled that it finally works out of the box. I tried comparing the previous XML code with the new one, and the only obvious one I saw was the addition of a <section/> tag at the end right after </prototypes>.

XCool
  • 976
  • 11
  • 32