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.
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...
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>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With