Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Note: I have added:

tableView.rowHeight = UITableViewAutomaticDimension

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

like image 885
Daniel Bramhall Avatar asked Jan 07 '23 13:01

Daniel Bramhall


2 Answers

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

http://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...

like image 164
Timothy Jason Williams Avatar answered Jan 15 '23 23:01

Timothy Jason Williams


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>.

like image 24
XCool Avatar answered Jan 15 '23 23:01

XCool