Ios UITableView中自定义单元格上的2个标签

Ios UITableView中自定义单元格上的2个标签,ios,uitableview,swift3,constraints,custom-cell,Ios,Uitableview,Swift3,Constraints,Custom Cell,我正在尝试在messenger应用程序中的邮件旁边添加日期/时间。目前,单元格的自动大小对于消息非常有用。但是,当我尝试向单元格添加第二个标签时,我得到一个运行时错误,即终止时出现未捕获异常。我试图在单元格的左侧设置日期,当前消息文本位于单元格的右侧。删除约束将使其永远不会显示时间标签 下面是CellForRowatineXpath方法的大部分内容 cell.myLabel.font = font cell.myLabel.numberOfLines = 0

我正在尝试在messenger应用程序中的邮件旁边添加日期/时间。目前,单元格的自动大小对于消息非常有用。但是,当我尝试向单元格添加第二个标签时,我得到一个运行时错误,即终止时出现未捕获异常。我试图在单元格的左侧设置日期,当前消息文本位于单元格的右侧。删除约束将使其永远不会显示时间标签

下面是CellForRowatineXpath方法的大部分内容

        cell.myLabel.font = font
        cell.myLabel.numberOfLines = 0
        cell.myLabel.text = self.messages[indexPath.row]

        cell.myLabel.textColor = UIColor.blue
        cell.myLabel.textAlignment = .justified

        let marginGuide = cell.contentView.layoutMarginsGuide
        cell.myLabel.translatesAutoresizingMaskIntoConstraints = false
        cell.myLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true
        cell.myLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor).isActive = true

        cell.myLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor, constant: 0).isActive = true
        cell.myLabel.backgroundColor = UIColor(patternImage: UIImage(named: "background")!)

        if size1.width > 260 {
            cell.myLabel.preferredMaxLayoutWidth = 260
        }
        else{
            cell.myLabel.preferredMaxLayoutWidth = size1.width
        }
        let interval = self.timeStamps[indexPath.row]
        if let myDouble = NumberFormatter().number(from: interval)?.doubleValue {
            let date = NSDate(timeIntervalSince1970: myDouble)
            cell.timeLabel.font = font
            cell.timeLabel.text = "date"// String(describing: date)
            cell.timeLabel.translatesAutoresizingMaskIntoConstraints = false

            cell.timeLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true
            cell.timeLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor).isActive = true
            cell.timeLabel.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor).isActive = true
        }
这是我创建的自定义单元格的整个程序

import UIKit

class messageTableViewCell: UITableViewCell {

var myLabel = UILabel()
var background = UIImageView()
var timeLabel = UILabel()

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

    self.contentView.addSubview(background)
    self.contentView.addSubview(myLabel)
}

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

override func layoutSubviews() {
    super.layoutSubviews()
    myLabel.numberOfLines = 0
    myLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
    myLabel.sizeToFit()
    let marginGuide = contentView.layoutMarginsGuide
    myLabel.translatesAutoresizingMaskIntoConstraints = false
    myLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true
    myLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor).isActive = true
    myLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor, constant: 0).isActive = true
    myLabel.backgroundColor = UIColor(patternImage: UIImage(named: "background")!)

    timeLabel.numberOfLines = 0
    timeLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
    timeLabel.sizeToFit()



}

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
}

}

有人请帮我弄清楚如何添加第二个标签。我什么都试过了。

我犯了一个粗心的错误,我忘了在我的cell类中添加子视图。 我需要这句话:

self.contentView.addSubview(timeLabel)

哪一行代码导致了崩溃?我为timeLabel设置约束的那一行:cell.timeLabel.bottomAnchor.constraint(equalTo:marginGuide.bottomAnchor).isActive=true,您在控制台中获得了关于崩溃的任何信息吗?你知道原因是什么吗?上面写着“未捕获异常”,我不确定错误是什么。你是否尝试过在情节提要中设置单元格并在CellForRowatineXpath中将其排在队列中?更整洁