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