0
我试图在消息传递应用程序中添加消息旁边的日期/时间。目前,该单元的自动尺寸对于消息而言非常有效。但是,当我尝试向单元格添加第二个标签时,我得到一个以未捕获异常终止的运行时错误。我正在尝试在单元格左侧的当前消息文本位于单元格右侧的日期。删除约束使得timeLabel从不显示。2 UITableView中的自定义单元格上的标签
这里大部分的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
}
}
有人请帮我弄清楚如何添加第二个标签。我已经尝试了一切。
什么行代码导致崩溃? – nathan
我为timeLabel设置约束的行: cell.timeLabel.bottomAnchor.constraint(equalTo:marginGuide.bottomAnchor).isActive = true – lawndogg
您是否在控制台中获得有关崩溃的任何信息?你知道原因是什么吗? – nathan