1

I'm creating a UITableViewCell programmatically. Cant get it to work in iOS 7. iOS8 works great with UITableViewAutomaticDimension. In this simplified version, the cell has one label with variable number of lines. I'm trying to calculate the height to respond to the table view. I've tried many permutations of calls but have not been successful. Here's the code

This routine populate a cell.contentView with a label and constraints. I haven't subclassed for this cell. This is in a view controller that also manages the table view.

Here's the complete source for a table view controller. The single line and line with breaks show up ok, but the long line that should word wrap displays as a single line and ends up going off the right edge of the view.

This is being loaded programmatically from AppDelegate without a nib or storyboard.

import UIKit

class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.delegate = self
    self.tableView.dataSource = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}
func createSRView(hView: UIView, index: Int) {
    var testLabel = UILabel()
    testLabel.numberOfLines = 0
    testLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
    switch index {
    case 0:
        testLabel.text = "This is a test of one line"
    case 1:
        testLabel.text = "This\nis\na test of\nmultiple lines"
    case 2:
        testLabel.text = "This is a test of a really long line that should definitely wrap to several lines in the simulator"
    default:
        testLabel.text = "nothing interesting"
    }
    testLabel.setTranslatesAutoresizingMaskIntoConstraints(false)

    hView.addSubview(testLabel)
    hView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[testLabel]-|", options: nil, metrics: nil, views: ["testLabel": testLabel]))
    hView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[testLabel]-|", options: nil, metrics: nil, views: ["testLabel": testLabel]))
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell: UITableViewCell?
    cell = UITableViewCell()
    createSRView(cell!, index: indexPath.row)
    return cell!
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    // var protoCell = tableView.dequeueReusableCellWithIdentifier("myCell") as! UITableViewCell
    var protoCell = UITableViewCell()
    createSRView(protoCell.contentView, index: indexPath.row)
    protoCell.contentView.bounds = CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), 1000.0)
    protoCell.setNeedsUpdateConstraints()
    protoCell.updateConstraintsIfNeeded()
    protoCell.setNeedsLayout()
    protoCell.layoutIfNeeded()
    let height = protoCell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
    println("Height for row \(indexPath.row) = \(height)")
    return height + 1.0
}
}
David
  • 2,770
  • 5
  • 35
  • 43

0 Answers0