Multiline UILabel within a Static UITableViewCell on iOS 9

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.

You can use UITableView's automatic cell height. Just pin the label in the cell by using auto layout constraints.


tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44.0

Unfortunately, I have tried this and still no luck. I'm just confused as I'm using a Basic table view cell on a static table view yet it seems to be unecessarily hard. Do you have any ideas as to how to simply make a multiline label with dynamic content within a static table view?

In your storyboard file set the cell height to 0. It is a bug in Xcode 7.


Check my explanation here.

http://stackoverflow.com/questions/32558084/multiline-uilabel-within-a-static-uitableviewcell-on-ios-9/32816593#32816593

Multiline UILabel within a Static UITableViewCell on iOS 9
 
 
Q