Xcode - Swift - TableViewCell扩展中的UILabel

时间:2015-07-29 15:54:39

标签: ios xcode swift uitableview uilabel

我在自定义UITableViewCell中有一个名为" ScheduleCell"的UILabel,如下所示:

enter image description here

以下是ScheduleCell的代码:

import UIKit

class ScheduleCell: UITableViewCell {

@IBOutlet weak var titleLabel: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

我希望tileLabel根据其文本进行扩展,我希望ScheduleCell随之扩展。以下是titleLabel的属性:

enter image description here

enter image description here

enter image description here

enter image description here

以下是单元原型的属性,它链接到ScheduleCell类:

enter image description here

enter image description here

enter image description here

以下是ViewController的一些相关代码:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 160.0

func tableView(tableView: UITableView!,cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! ScheduleCell

    cell.backgroundColor = UIColor(rgb: 0xF6FBFE)

    cell.titleLabel.text = (Globals.scheduleArr[indexPath.row][3] as! String)
    cell.titleLabel.setTranslatesAutoresizingMaskIntoConstraints(false)

    return cell
}

所有标题来自" Globals.scheduleArr"适合排在一条线外,其中一条是“简·理查兹格雷读书"杀死一只知更鸟""。以下是ViewController在iOS模拟器中的样子:

enter image description here

如您所见,标签不会根据需要展开。奇怪的是,当我添加一个" \ n"在每个标题的末尾,标签确实扩展了。但是,单元格似乎没有随着标签扩展:

enter image description here

非常感谢您解决此问题的任何帮助!

1 个答案:

答案 0 :(得分:1)

如果您想使用UITableViewAutomaticDimension,您需要添加Label-ContentView 底部约束

添加新的底部约束,然后重试!