Ios 具有两个UILabel的自动调整UITableViewCell大小

Ios 具有两个UILabel的自动调整UITableViewCell大小,ios,uitableview,Ios,Uitableview,我正在尝试使用自动布局调整UITableViewCell的大小,以始终适合其内容 每个单元格包含两个垂直堆叠的标签topLabel和bottomLabel。我希望两个标签都包装并显示其所有文本 我用的是iOS8 这是我的密码 class MyTableViewCell: UITableViewCell { @IBOutlet var topLabel: UILabel! @IBOutlet var bottomLabel: UILabel! override init(

我正在尝试使用自动布局调整UITableViewCell的大小,以始终适合其内容

每个单元格包含两个垂直堆叠的标签
topLabel
bottomLabel
。我希望两个标签都包装并显示其所有文本

我用的是iOS8

这是我的密码

class MyTableViewCell: UITableViewCell {
    @IBOutlet var topLabel: UILabel!
    @IBOutlet var bottomLabel: UILabel!

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

        super.init(style: style, reuseIdentifier: reuseIdentifier)

        topLabel = UILabel()
        topLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
        topLabel.numberOfLines = 0
        contentView.addSubview(topLabel)

        bottomLabel = UILabel()
        bottomLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
        bottomLabel.numberOfLines = 0
        contentView.addSubview(bottomLabel)

        let viewDictionary = ["topLabel": topLabel, "bottomLabel": bottomLabel]
        var constraints = [NSLayoutConstraint]()

        contentView.addConstraints(
            NSLayoutConstraint.constraintsWithVisualFormat(
                "H:|-[topLabel]-|",
                options:NSLayoutFormatOptions(0),
                metrics: nil,
                views: viewDictionary))

        contentView.addConstraints(
            NSLayoutConstraint.constraintsWithVisualFormat(
                "H:|-[bottomLabel]-|",
                options:NSLayoutFormatOptions(0),
                metrics: nil,
                views: viewDictionary))

        contentView.addConstraints(
            NSLayoutConstraint.constraintsWithVisualFormat(
                "V:|-[topLabel]-[bottomLabel]-|",
                options:NSLayoutFormatOptions(0),
                metrics: nil,
                views: viewDictionary))

    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class MyTableViewController: UITableViewController, UITableViewDataSource {

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

        return 5;
    }

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

    override func tableView(
        tableView: UITableView, 
        cellForRowAtIndexPath indexPath: NSIndexPath) ->
        UITableViewCell {

        let cell = MyTableViewCell(
            style: UITableViewCellStyle.Default, 
            reuseIdentifier: nil)

        cell.topLabel.text = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26"
        cell.bottomLabel.text = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"

        return cell
    }
}
但是,只有顶部标签的文本换行。结果是


我不确定我是否弄乱了我的约束,或者我是否应该设置另一个属性,或者这是不可能的。非常感谢您的帮助。

首先,您应该使用table view delegate
heightForRowAtIndexPath
返回该行的单元格高度。其次,请将单元格中标签的行数设置为零。若你们有确认约束并返回正确的单元格高度,那个么你们将得到你们想要的


谢谢

在您的
cellForRowAtIndexPath
中添加这一行似乎可以解决问题

cell.layoutIfNeeded()

恐怕我现在不能给你代码,但会在我到达我的位置时更新。谢谢!知道为什么会这样吗?为什么我需要调用layoutIfNeeded?请参考此,它基本上要求单元格立即布局其子视图(底部标签)。在这方面,也使用了这种技术。