2 UITableView中自定义Cell的标签

时间:2017-04-24 00:51:35

标签: ios uitableview swift3 constraints custom-cell

我正在尝试在Messenger应用中添加消息旁边的日期/时间。目前,单元格的自动尺寸对于消息非常有用。但是,当我尝试向单元格添加第二个标签时,我得到一个运行时错误,终止了未捕获的异常。我试图在单元格的左侧创建日期,当前消息文本位于单元格的右侧。删除约束使得从不显示timeLabel。

这是大多数cellforRowatIndexPath方法

        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
}

}

有人请帮我弄清楚如何添加第二个标签。我已经尝试了一切。

1 个答案:

答案 0 :(得分:0)

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

self.contentView.addSubview(timeLabel)